Save to CSV example in Python

If you are dealing with data using Python, at some point, you might need to export the data into CSV format. In this post, I will share some example how you can save the data into CSV file.

What is CSV?

CSV is Comma Separated Values. Basically it is a file with collection of data separated by a comma. File itself can be opened on Text Editor and you will see some details like below:

name,age,city,state
Joe,23,San Francisco,CA
Mike,45,San Jose,CA
Jane,29,Seattle,WA

If you save the above text as “.csv” file, it will recognize as “CSV” file. Spreadsheet app such as Excel can read this type of file and it can recognize as spreadsheet. When you open this type of file on Excel, each text will be put into cell. Spreadsheet will look like below:

nameagecitystate
Joe23San FranciscoCA
Mike45San JoseCA
Jane29SeattleWA

How to Create CSV file from Python

It is pretty easy to create a CSV file from Python. On a high level, you basically need to create a file with extension name of .csv. You can achieve this by using the import os and with the use of file open/write.

Below codes (after the explanation) show some example of creating strings and it appends to the CSV file. When you execute this Python file, it will create a file called “sample_output_to_csv.csv” file.

Important lines are highlighted. which is:

import os

import os is needed to be able to interact with the OS’ filesystem.

file_name = "sample_output_to_csv.csv"

[file_name] is used to hold the file name. In this case, “sample_output_to_csv.csv” Don’t forget to include the extension name (.csv)

f = open(file_name, "a")

f = open(file_name, “a”) is to initiate the interaction with file assigned with file_name value. “a” means, you want to append the data. You want to use append in this example since you are adding series of data into a file.

f.write(file_string)

f.write is basically you want the program to actually write the data into the file.

import os
import random
r_size = int(10) # set the number of times you want to iterate
file_name = "sample_output_to_csv.csv" # file name to be used when saving as CSV file
# file_string value below will be stored as the first line in the CSV file
# first line is usually the header row
file_string = "UID,NUM_VALUE,NOTES\n"
# use open() to initiate some action with the file
# "a" means append. You want to append the data instead of overwriting it
f = open(file_name, "a")
# write() to actually write the data into the file
f.write(file_string)
for _ in range(r_size):
random_number = random.randint(1, 9999)
# file_string below is representing the row
file_string = f"{_},{random_number},The quick brown fox jumps over the lazy dog\n"
# It opens the file assigned in file_name and it is preparing to "append" the data
f = open(file_name, "a")
# write() will store the data in file_string to CSV file
f.write(file_string)

If you know how to extract the data to CSV, you can collect some data and analyze it in the Spreadsheet app and you can also share the data to non-technical folks. It is pretty easy to export to CSV and pretty useful for analysis.

Recent Posts

Feel free to share this post!

Scroll to Top