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 from a list into a CSV file
To write text line by line into a csv file using Python, proceed as following:
- Import the csv module into your Python script.
- Gather the data to be written into the csv file ; in our case the data source is a Python list, but it could be also a dictionary, text from an external file or a database table.
- Define a path in your operating system for your csv
- Create a file object with write (‘w’) permission.
- Create a new instance of a csv.writer object.
- Loop through the data that you would like to add to the csv, and using the writerow method add the text line by line.
Here’s the code:
import csv from pathlib import Path # define lists containing rows to append to the csv 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')