Home >Backend Development >Golang >Use the functions provided by the encoding/csv package to read and write CSV files

Use the functions provided by the encoding/csv package to read and write CSV files

WBOY
WBOYOriginal
2023-07-24 13:13:121328browse

Use the functions provided by the encoding/csv package to read and write CSV files

CSV (Comma-Separated Values) is a commonly used data storage format that can simply separate data in comma-separated form Save as text file. In Python, you can use the encoding/csv package in the standard library to conveniently read and write CSV files.

First, we need to import the encoding/csv package:

import csv

Next, we can use the csv.reader function to read the CSV file . csv.readerThe function accepts a file object as a parameter and returns an iterator object, which can be used to read the CSV file line by line.

The following is an example, assuming we have a CSV file named data.csv, which contains the following data:

Name,Age,City
John,25,New York
Lisa,30,San Francisco
David,40,Los Angeles

We can use csv .reader function to read the data of the file:

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

Output result:

['Name', 'Age', 'City']
['John', '25', 'New York']
['Lisa', '30', 'San Francisco']
['David', '40', 'Los Angeles']

As you can see, the data of each row is returned in the form of a list. Among them, the first line is the header of the CSV file, followed by the data lines.

In addition to using the csv.reader function to read CSV files, we can also use the csv.writer function to write CSV files.

The following is an example, assuming we have an empty file named data.csv, we can use the csv.writer function to write data to the file:

data = [
    ['Name', 'Age', 'City'],
    ['John', '25', 'New York'],
    ['Lisa', '30', 'San Francisco'],
    ['David', '40', 'Los Angeles']
]

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

Note that when writing a CSV file, we use the newline='' parameter to avoid generating blank lines.

The above code writes data to the data.csv file. If you open the file, you will find that its contents are the same as the CSV file we read earlier.

In addition to basic reading and writing, the encoding/csv package also provides other functions and options to achieve more advanced functions. For example, you can use the csv.DictReader and csv.DictWriter functions to perform dictionary-style reading and writing operations on CSV files.

To summarize, using the functions provided by the encoding/csv package, you can easily read and write CSV files. Not only that, the encoding/csv package also provides more advanced functions to meet various complex needs. If you need to process CSV files, the encoding/csv package is definitely one of your first choices.

The above is the detailed content of Use the functions provided by the encoding/csv package to read and write CSV files. 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