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 field content lines.

'Name','Email','Title'
'jerry','jerry@dev2qa.com','CEO'
'tom','tom@dev2qa.com','Developer'

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

1. Python Write CSV File.

  1. 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 the Python csv module.
    import csv
  2. Open a file with write permission.
    file_object = open(file_path, 'w')
  3. Call the csv.writer method to create a CSV file writer. The method’s first parameter is the CSV file object. The second parameter is the delimiter, the quoting parameter tells the method when to add a quote to file filed name and value, the quotechar parameter tells 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 the python csv module and the 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 the csv.DictWriter method to create a DictWriter object. The first parameter is the file object, the fieldnames parameter is a python list data that contains the CSV filed names.
    csv_dict_file_writer = csv.DictWriter(file_object, fieldnames=column_name_list)
  4. Call the python csv module’s DictWriter object’s writeheader() method to write the CSV file filed names.
    csv_dict_file_writer.writeheader()
  5. Call the python csv module’s DictWriter object’s writerow() method to write a python dictionary data to the file.
    csv_dict_file_writer.writerow(dict_data)

2. Python Read CSV File.

  1. Python csv module provides two readers 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 the Python csv module.
    import csv
  2. Open CSV file with the 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 a 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 the 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 the 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.

  1. This example will show the detailed source code for the CSV file read/write operation.
  2. There are two python classes and two global functions in the example.
  3. The python file name is CSVReadWriteExample.py.
  4. CSVFileOperator: This python class provide functions for CSV file read/write operation. The two CSV files ( csv_coding_language.csv, csv_user_info.csv ) in this example are the result files this class’s write method creates.
  5. TestCSVFileOperator: This is a python unittest.TestCase class which provides a unit test for the CSVFileOperator methods. You can read Python 3 Unittest Html And Xml Report Example to learn more.
  6. run_all_test_case: This global function will run all test methods in the TestCSVFileOperator class.
  7. run_special_test_case(test): This function will run the specified test method of TestCSVFileOperator class provided by the function parameter.
  8. CSVReadWriteExample.py
    '''
    @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 = 0
    
              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 = 0
              # 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','tom@dev2qa.com','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

Leave a Comment

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.