Home >Backend Development >Python Tutorial >How to use the csv module to read and write CSV files in Python 3.x

How to use the csv module to read and write CSV files in Python 3.x

王林
王林Original
2023-07-29 23:49:121803browse

How to use the csv module to read and write CSV files in Python 3.x

Introduction:
CSV (Comma-Separated Values) is a common file format used to store tabular data . In Python, CSV files can be processed using the csv module, which provides functions for reading and writing CSV files. This article explains how to use the csv module to read and write CSV files in Python 3.x, and provides code examples.

1. Use the csv module to read CSV files

First, we need to import the csv module:

import csv

Next, we can use the csv.reader() function to read Get the CSV file. This function needs to pass in a file object. We can use the open() function to open a CSV file and pass the file object as a parameter to the csv.reader() function.

Suppose we have a CSV file named data.csv with the following content:

name,age,city
John,25,New York
Peter,30,Chicago
Sarah,28,Los Angeles

The following is a simple code example showing how to use the csv module to read a CSV file :

import csv

with open('data.csv', 'r') as file:
    csv_reader = csv.reader(file)
    for row in csv_reader:
        print(row)

Run the above code and each row of data in the CSV file will be output. In this example, we use a for loop to iterate over the csv_reader object to print the data in the CSV file line by line.

2. Use the csv module to write CSV files

Similar to reading CSV files, we can also use the csv module to write CSV files. You also need to import the csv module:

import csv

Then, we can use the csv.writer() function to create a csv_writer object, which can write data to the CSV file. This function needs to pass in a file object. You can also use the open() function to open a CSV file and pass the file object as a parameter to the csv.writer() function.

The following is a simple code example showing how to use the csv module to write a CSV file:

import csv

data = [
    ['name', 'age', 'city'],
    ['John', '25', 'New York'],
    ['Peter', '30', 'Chicago'],
    ['Sarah', '28', 'Los Angeles']
]

with open('output.csv', 'w', newline='') as file:
    csv_writer = csv.writer(file)
    csv_writer.writerows(data)

Running the above code will create a file named output.csv in the current directory. CSV file and write the data in data to the file.

In the code example, we first define a two-dimensional list data, which contains each row of data in the CSV file. We then use the csv.writerows() function to write the entire list to a CSV file.

Summary:
Through the csv module, we can easily read and write CSV files in Python 3.x. When reading a CSV file, we can use the csv.reader() function and for loop to process the data line by line; when writing a CSV file, we can use the csv.writer() function and csv_writer.writerows() function to The data is written into a CSV file.

I hope this article will help you use the csv module in Python to read and write CSV files. Happy studying!

The above is the detailed content of How to use the csv module to read and write CSV files in Python 3.x. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn