Python Read/Write CSV File Example

CSV is the abbreviation of Comma Separated Values, CSV file is a text file contains CSV format content. Below is an example of CSV file content, it is so simple. We can see that there are three lines in the file, the first line is the header field name line, there are three fields. The second and the third line are related filed content line.

'Name','Email','Title'
'jerry','jerry@dev2qa.com','CEO'
'tom','[email protected]','Developer'

Python built-in csv module provide methods to read and write CSV files. This article will just give you some examples.

1. Python Write CSV File.

First we will tell you how to write data to a CSV file. You can either write a python list or dictionary type data into a CSV file.

1.1 Write Python List Data To CSV File Steps.

  1. Import csv module.
    import csv
  2. Open a file with write permission.
    file_object = open(file_path, 'w')
  3. Call csv.writer method to create a csv file writer. The method first parameter is the csv file object. The second parameter is the delimiter, the quoting parameter tell the method when to add quote to file filed name and value, the quotechar parameter tell the method the quote character to use (generally ‘ or “).
    csv_file_writer = csv.writer(file_object, delimiter=",", quoting=csv.QUOTE_ALL, quotechar="'")
  4. Call csv_file_writer’s writerow() method to write a python list data to the file.
    csv_file_writer.writerow(list_data)

1.2 Write Python Dictionary Data To CSV File Steps.

  1. Import python csv module and csv.DictWriter class.
    import csv
    from csv import DictWriter
  2. Open the target csv file with write permission.
    file_object = open(file_path, 'w')
  3. Call csv.DictWriter method to create a DictWriter object. The first parameter is the file object, the fieldnames parameter is a python list data which contains the csv filed names.
    csv_dict_file_writer = csv.DictWriter(file_object, fieldnames=column_name_list)
  4. Call csv DictWriter writeheader() method to write the csv file filed names.
    csv_dict_file_writer.writeheader()
  5. Call csv DictWriter writerow() method to write a python dictionary data to the file.
    csv_dict_file_writer.writerow(dict_data)

2. Python Read CSV File.

Python csv module provide two reader to return csv file content in either python list data or python dictionary data.

2.1 Read CSV File Return List Data Steps.

  1. Import csv module.
    import csv
  2. Open CSV file with read permission.
    file_object = open(file_path, 'r')
  3. Call csv.reader method to get a reader, this reader will return CSV file data in list.
    csv_file_reader = csv.reader(file_object, delimiter=',')
  4. Loop in the returned csv_file_reader, each item in the reader is python list type data.
    for row in csv_file_reader:
          ......

2.2 Read CSV File Return Dictionary Data Steps.

  1. Import python csv module and csv.DictWriter class.
    import csv
    from csv import DictWriter
  2. Open CSV file with read permission.
    file_object = open(file_path, 'r')
  3. Create a csv.DictReader object.
    csv_file_dict_reader = csv.DictReader(file_object)
  4. Call above DictReader’s fieldnames property to get all CSV file field names in a python list.
    field_names = csv_file_dict_reader.fieldnames
  5. Loop in above field_names list to get each CSV file field name.
  6. Loop in the csv.DictReader object to get each row data in a python dictionary type. For each row, loop in the fieldnames to get each field related value.
    for row in csv_file_dict_reader:
          ......

3. Python Read/Write CSV File Example.

This example will show the detail source code for CSV file read/write operation. There are two python class and two global function in the example.

python read write csv file example source file

  1. CSVFileOperator : This python class provide functions for csv file read / write operation. The two csv files in above picture are the result files this class write method created.
  2. TestCSVFileOperator : This is a python unittest.TestCase class which provide unit test for the CSVFileOperator methods. You can read Python 3 Unittest Html And Xml Report Example to learn more.

  3. run_all_test_case : This global function will run all test methods in the TestCSVFileOperator class.

  4. run_special_test_case(test) : This function will run the specified test method of TestCSVFileOperator class provided by the function parameter.

READ :   Python Pandas Read/Write CSV File And Convert To Excel File Example

CSVReadWriteExample.py

'''
Created on Aug 24, 2018
@author: zhaosong
'''

import csv
import unittest
from csv import DictWriter

# This is the csv file read/write class.
class CSVFileOperator(object):

    line_return_character = '\r\n'
    comma_separator = ','

    # Write a data_list into a csv file identified by file_path.
    # Each element in the data_list is also a list object represent one row.    
    def write_list_to_csv_file(self, data_list, file_path):

        try:
            file_object = open(file_path, 'w')
            csv_file_writer = csv.writer(file_object, delimiter=",", quoting=csv.QUOTE_ALL, quotechar="'")

            for item in data_list:
                csv_file_writer.writerow(item)

            print(file_path + " has been created successfully.")
        except FileNotFoundError:
            print(file_path + " not found.")

    # Write a data_list into a csv file identified by file_path.
    # Each element in the data_list is a dictionary object represent one row.    
    def write_dict_to_csv_file(self, data_list, column_name_list, file_path):
        try:
            file_object = open(file_path, 'w')
            csv_dict_file_writer = csv.DictWriter(file_object, fieldnames=column_name_list)
            csv_dict_file_writer.writeheader()

            for item in data_list:
                csv_dict_file_writer.writerow(item)                

            print(file_path + " has been created successfully.")
        except FileNotFoundError:
            print(file_path + " not found.")

    # Read csv file line by line and return the content.
    def read_csv_file_by_reader(self, file_path):
        ret = ''
        try:
          file_object = open(file_path, 'r')
          csv_file_reader = csv.reader(file_object, delimiter=',')
          row_number = 

          for row in csv_file_reader:
              # Get one row list data string value,
              # below code can avoid type convert error if column has number or boolean value.
              row_str = ''
              row_size = len(row)
              for i in range(row_size):
                  row_str += str(row[i]) + self.comma_separator

              print("row_" + str(row_number) + " = " + row_str)
              ret += row_str
              ret += self.line_return_character
              row_number += 1
        except FileNotFoundError:
            print(file_path + " not found.")
        finally:
          print("ret = " + ret)
          return ret             
            
    # Read csv file line by line and return the content.
    def read_csv_file_by_dict_reader(self, file_path):
        ret = ''
        try:
          file_object = open(file_path, 'r')
          # csv.DictReader will return a dictionary list.
          csv_file_dict_reader = csv.DictReader(file_object)

          # Get csv file fields name list.
          field_names = csv_file_dict_reader.fieldnames

          # Get field name list size.
          field_name_size = len(field_names)

          # Get each field name.
          for i in range(field_name_size):
              field_name = field_names[i]
              ret += field_name + self.comma_separator
          
          # Add line return character.     
          ret += self.line_return_character
          print("Field Names : " + ret)

          row_number = 
          # Each row is one dictionary.
          for row in csv_file_dict_reader:
              row_str = ''
              # Loop the row field name.
              for i in range(field_name_size):
                  field_name = field_names[i]
                  # Get field value in this row. Convert to string to avoid type convert error.
                  row_str += str(row[field_name]) + self.comma_separator

              print("row_" + str(row_number) + " = " + row_str)
              ret += row_str
              ret += self.line_return_character
              row_number += 1
        except FileNotFoundError:
            print(file_path + " not found.")
        finally:
          print("ret = " + ret)
          return ret         

# This is the test case class for CSVFileOperator class.            
class TestCSVFileOperator(unittest.TestCase):

    # Test CSVFileOperator's write_list_to_csv_file.
    def test_write_list_to_csv_file(self):
        data_row_header = ['Name','Email','Title']
        data_row_1 = ['jerry','jerry@dev2qa.com','CEO']
        data_row_2 = ['tom','[email protected]','Developer']
        data_list = [data_row_header, data_row_1, data_row_2]
        csv_file_operator = CSVFileOperator()
        csv_file_operator.write_list_to_csv_file(data_list, "./csv_user_info.csv")    

    # Test CSVFileOperator's write_dict_to_csv_file.    
    def test_write_dict_to_csv_file(self):
        # First create some dictionary type data.
        data_row_header = ['Coding Language','Popularity']
        data_row_1 = {'Coding Language':'Java','Popularity':'17797'}
        data_row_2 = {'Coding Language':'JavaScript','Popularity':'18998'}
        data_row_3 = {'Coding Language':'Python','Popularity':'29898'}
        data_row_4 = {'Coding Language':'C++','Popularity':'16567'}
        data_row_5 = {'Coding Language':'C','Popularity':'9989'}
        data_row_6 = {'Coding Language':'Html','Popularity':'18983'}

        # Add dictionary data into a list
        data_list = [data_row_1, data_row_2, data_row_3, data_row_4, data_row_5, data_row_6]        
        csv_file_operator = CSVFileOperator()
        csv_file_operator.write_dict_to_csv_file(data_list, data_row_header, "./csv_coding_language.csv")
        
    def test_read_csv_file_by_reader(self):
        csv_file_operator = CSVFileOperator()
        csv_file_operator.read_csv_file_by_reader("./csv_coding_language.csv")
        
    def test_read_csv_file_by_dict_reader(self):
        csv_file_operator = CSVFileOperator()
        csv_file_operator.read_csv_file_by_dict_reader("./csv_coding_language.csv")  

def run_all_test_case():
    unittest.main()

def run_special_test_case(test):
    # Create a TestSuite object.
    test_suite = unittest.TestSuite()
    # Add test.
    test_suite.addTest(test)
    # Create a TestResult object to save test result info.
    test_result = unittest.TestResult()
    # Run the test suite.
    test_suite.run(test_result)    

if __name__ == '__main__':
    #run_all_test_case()
    #run_special_test_case(TestCSVFileOperator("test_read_csv_file_by_reader"))
    #run_special_test_case(TestCSVFileOperator("test_read_csv_file_by_dict_reader"))    
    #run_special_test_case(TestCSVFileOperator("test_write_list_to_csv_file"))
    run_special_test_case(TestCSVFileOperator("test_write_dict_to_csv_file"))

Reference

  1. Python 3 Unittest Html And Xml Report Example
(Visited 371 times, 1 visits today)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.