This tutorial explains how to use Python 3 to write data line by line into a comma separated value file. We’ll look into several examples:
- Using the csv module.
- Using the Pandas Data Analysis library
Write text using the CSV module
In the first example we’ll use the Python csv library to define a writer object that we’ll use to modify our file:
import csv from pathlib import Path # data to write data =[['Paris', 150],['London', 200] ] header_row = ['office_name', 'num_employees'] # define target file to write into dir_path = Path('C:\WorkDir') file_name = 'hr.csv' file_path = dir_path.joinpath(file_name) # write into the csv file with open (file_path, 'w', newline='') as csv_file: csv_writer = csv.writer(csv_file) for row in data: csv_writer.writerow(row)
Common errors you might encounter
- UnsupportedOperation: not writable: This error will be triggered if you haven’t specified that the csv file should be open in write (‘w’) mode.
- [Errno 13] Permission denied: <file_name_here>: This error will be triggered if the file you are trying to access is already opened. Manually close your file and then re-run your Python program.
Using Pandas to write and modify the csv file
Pandas is an amazing library, also with regards to working with files. The following short snippet allows to write line by line into the csv file.
Important Note: before using the Pandas library you’ll need to pip install it and then import into your development workspace. Look into the following post to troubleshoot any errors.
import pandas as pd # create a DataFrame - using the data and headers hr_df = pd.DataFrame(data, columns = header_row) # export the data to a csv file on your computer hr_df.to_csv('hr.csv')