reading and writing CSV files in Python

CSV is ‘Comma Separate Values’ file. As the name implies, the content of the document is composed of a column of data separated by “,”. CSV file is a data storage method that is easy to edit and has excellent visual effects. 

Python has a very powerful library that can handle such documents, so if you often use Python to process data, CSV documents are of course a simple and lightweight choice. Below I will start with an example to show the creation and editing of CSV documents, and how Python reads and writes CSV documents.

The biggest advantage of CSV documents is that they can interact with Excel easily. We can easily create, view and edit CSV documents in Excel. After editing the data in Excel, save the file using .csv format, and you are done.

How to read CSV file in Python

The Python library csv is a very powerful library for processing CSV documents. If we want to process CSV documents, we must first import it.

import csv

_file = open("customer.csv", "r")
file_reader = csv.reader(_file)

data = {}
for row in file_reader:
    if file_reader.line_num == 1:
        continue
    data[row[0]] = row[1]

_file.close()
print(data)

First of all, like a any other text file, you need open()function to open the file, I will open the file in read-only mode. Then use csv.reader() to create an object of a data read function.

The file_reader object is actually composed of multiple lines of data in a CSV file. Each line of data has an attribute: line_num represents the number of lines. Obviously, the first line of data is the row name and can be ignored.

If you use a for loop to access the file_reader iteratively, you will find that each row is a list, and each element of the list is the data separated by “,”:

for row in file_reader:
    print(row) # >>> ['item', 'code']
                # >>> ['item1', 'code1']
                # >>> ['item2', 'code2']
                # >>> ['item3', 'code3']

You can also directly traverse the document like a text files, but what you get is a string instead of a list of corresponding elements in each row. Obviously, it is more convenient to read it as a list for processing data.

How to Write CSV file in Python

import csv

row_header = ["item", "code"]
data = ["item4", "code4"]

_file = open("customer.csv", "w")
file_writer = csv.writer(_file)

file_writer.writerow(row_header)
file_writer.writerow(data)

_file.close()

In case of writing multiple lines, you can use a more convenient function writerows(), or the above example, you can replace the two lines of code with the following line: file_writer.writerows([row_header, data]) . You can also append the file using a in the open() parameter. To append the csv file in Python, use the following code:

import csv

data = ["item5", "code5"]

_file = open("customer.csv", "a")
file_writer = csv.writer(_file)

file_writer.writerow(data)

_file.close()

Use DictReader with CSV file

Save a relational database as a CSV file, and then read it with Python. The processing situation is very common if you are using file based database.

Most of them are read into a dictionary first, and then do the corresponding calculations. So the csv library also provides a function that can directly read CSV documents as a dictionary:, DictReader(), of course, there is corresponding DictWriter() to write back to csv file.

import csv

_file = open("customer.csv", "r")
dict_reader = csv.DictReader(_file)

for row in dict_reader:
    print(row)
## result
{'item':'item1', 'code':'code1'}
{'item':'item2', 'code':'code2'}
{'item':'item3', 'code':'code3'}
{'item':'item4', 'code':'code4'}

To get all the fields name, use fieldnames attribute print(dict_reader.fieldnames) # >>> ['item', 'code']

You can easily convert dict_reader object into the ordinary Python dictionary in 3 lines of code

data = {}
for row in dict_reader:
    data[row["item"]] = row["code"]
print(data) # >>> {'item1': 'code1', 'item2': 'code2', 'item3': 'code3'}

Use DictWriter with CSV file

import csv

_file = open("customer.csv", "w")

header = ["item", "code"]
dict_writer = csv.DictWriter(_file, header)

dict_writer.writerow(dict(zip(header, header)))

dict_writer.writerow({"item": "item1", "code": "code1"})
csvFile.close()
Scroll to Top