Teach you how to read, write and parse CSV text in Python

Abstract: in this article about "how to read CSV files in Python", we will learn how to read, write and parse CSV files in Python.

Do you know the mechanism behind storing table data into plain text files? The answer is a CSV (comma separated values) file that allows data to be converted to plain text format. In this article on "how to read CSV files in Python", we will learn how to read, write and parse CSV files in Python.

The following aspects will be discussed in detail:

    • What is a CSV file and its purpose?
    • Why use CSV file format?
    • Python CSV module
      • CSV module function
    • Write and read CSV files in Python

Let's start.

What is a CSV file and its purpose?

CSV (comma separated values) is a plain text file format used to store tabular data, such as spreadsheets or databases. It essentially stores tabular data, including numbers and plain text. Most online services give users the freedom to export data from a web site to a CSV file format. CSV files are usually opened in Excel, and almost all databases have different specific tools to allow the import of the same file.

Each line of a file is called a record. Each record consists of fields separated by commas. These fields are also called "delimiters", which are the default delimiters. Other records include pipe (|) and semicolon (;). The following is the structure of an ordinary CSV file separated by commas. I am using a titanic CSV file.

structure

Passenger,Id,Survived,Pclass,Name,Sex.Age
1,0,3 Braund, Mr. Owen Harris ,male, 22
2,1,1 Cumings, Mrs. John Bradley (Florence Briggs Thayer), female,38
3,1,3 Heikkinen, Miss. Laina ,female, 26
4,1,1 Futrelle, Mrs. Jacques Heath (Lily May Peel),female,35

Continue to talk about the reasons for using CSV file format.

Why use CSV file format?

CSV is a plain text file that makes data exchange easier and easier to import into spreadsheet or database storage. For example, you may want to export the data of a statistical analysis to a CSV file and then import it into a spreadsheet for further analysis. Overall, it allows users to easily experience work through programming. Any language that supports text files or string operations (such as Python) can use CSV files directly.

Moving on, let's see how Python uses CSV natively.

Python CSV module

The CSV package used by Python is part of the standard library, so you don't need to install it.

import csv

Now let me show you the different CSV functions.

CSV module function

Under the CSV module, you can find the following functions:

Let's move on from the coding perspective of different operations on Python CSV files.

Operation of CSV file in Python

After loading the CSV file, you can perform a variety of operations. I will show the read and write operations to CSV files in Python.

To read CSV files in Python:

import csv 
 
with open('Titanic.csv','r') as csv_file: #Opens the file in read mode
    csv_reader = csv.reader(csv_file) # Making use of reader method for reading the file
 
    for line in csv_reader: #Iterate through the loop to read line by line
        print(line)

Output:

Here, you can see from the output that I have used the Titanic CSV File. And all fields are separated by commas, and the file is read into Python.

Moving on, let's see how to write a CSV file.

To write a CSV file in Python:

import csv
 
with open('Titanic.csv', 'r') as csv_file:
    csv_reader = csv.reader(csv_file)
 
    with open('new_Titanic.csv', 'w') as new_file: # Open a new file named 'new_titanic.csv' under write mode
        csv_writer = csv.writer(new_file, delimiter=';') #making use of write method
 
        for line in csv_reader: # for each file in csv_reader
            csv_writer.writerow(line) #writing out to a new file from each line of the original file

out:

Now, this method of processing CSV files using reader writer method is one of the most common methods. Let's move on and see how to use the python dictionary to do the same thing.

Read CSV file as Dictionary:

import csv 
 
with open('Titanic.csv','r') as csv_file: #Open the file in read mode
    csv_reader = csv.DictReader(csv_file) #use dictreader method to reade the file in dictionary
 
    for line in csv_reader: #Iterate through the loop to read line by line
        print(line)

Output:

As you can see from the output, the fields have been replaced and they now act as the "key" of the dictionary.

Let's see how to write CSV files as dictionaries.

Write CSV file as dictionary

import csv 
 
mydict = [{'Passenger':'1', 'Id':'0', 'Survived':'3'}, #key-value pairs as dictionary obj
          {'Passenger':'2', 'Id':'1', 'Survived':'1'},
          {'Passenger':'3', 'Id':'1', 'Survived':'3'}]
 
fields = ['Passenger', 'Id', 'Survived'] #field names
 
filename = 'new_Titanic.csv' #name of csv file
 
with open('new_Titanic.csv', 'w')as new_csv_file: #open a new file 'new_titanic,csv' under write mode
    writer = csv.DictWriter(new_csv_file, fieldnames=fields) 
    writer.writeheader() #writing the headers(field names)
 
    writer.writerows(mydict) #writing data rows

Output:

Let's see how to read CSV files as pandas in python.

Read CSV file in Panda format:

import pandas #install pandas package
 
result = pandas.read_csv('Titanic.csv') #read the csv file
 
print(result) # print result

Output:

This brings us to the end of the article "how to read CSV files in Python". I hope you have a clear understanding of all concepts related to CSV, how to read and write it, how to read and write CSV as a dictionary and how to read CSV as a panda.

Make sure to practice as much as possible and restore experience.

This article is shared from Huawei cloud community "how to read CSV files in Python?", Original author: Yuchuan.

 

Click follow to learn about Huawei's new cloud technology for the first time~

Tags: Python

Posted by kotun on Sat, 16 Apr 2022 03:54:51 +0930