Python export CSV file
Exporting CSV Files with Python
What is a CSV File?
A CSV file (Comma-Separated Values) is a simple, universal file format for storing tabular data. Each line in a CSV file represents a data record, and each record consists of multiple fields separated by commas. CSV files can be opened with spreadsheet software such as Excel or viewed and edited with a text editor. Because CSV files are simple and easy to understand, they are often used for data exchange and storage.
For example, the following is a simple CSV file:
name,age,gender
Tom,28,male
Mary,25,female
Exporting CSV Files with Python
The csv
module in the Python standard library provides a series of methods and tools for reading and writing CSV files, making it easy to exchange and process data. Exporting a CSV file using the csv
module is simple; just follow the steps below.
Step 1: Prepare the Data
First, prepare the data to be written to the CSV file, typically a nested structure of lists or tuples. For example, consider the following data:
students = [
("Tom", 28, "male"),
("Mary", 25, "female"),
("John", 32, "male")
]
Step 2: Open the CSV File
Use the open()
function to open the CSV file to be written, set the file mode to write, and specify the encoding. The sample code is as follows:
import csv
with open("students.csv", "w", encoding="utf-8", newline="") as csvfile:
writer = csv.writer(csvfile)
Here, newline=""
specifies that the newline character is empty to prevent extra blank lines when writing to the file.
Step 3: Writing to a CSV File
Use the csv.writer()
function to create a writer
object, then call its writerow()
method to write the data row by row to the CSV file. The sample code is as follows:
import csv
students = [
("Tom", 28, "male"),
("Mary", 25, "female"),
("John", 32, "male")
]
with open("students.csv", "w", encoding="utf-8", newline="") as csvfile:
writer = csv.writer(csvfile)
writer.writerow(["name", "age", "gender"]) # Write the header row
for student in students:
writer.writerow(student)
The writer.writerow()
method is used to write a row of data. In the above code, we first create a header row and then iterate through the data list, writing each student’s information to the CSV file.
Full Example Code
import csv
students = [
("Tom", 28, "male"),
("Mary", 25, "female"),
("John", 32, "male")
]
with open("students.csv", "w", encoding="utf-8", newline="") as csvfile:
writer = csv.writer(csvfile)
writer.writerow(["name", "age", "gender"]) # Write the header row
for student in students:
writer.writerow(student)
If the program runs successfully, you should find a file named students.csv
in the directory and open it to view its contents.
Conclusion
Using the csv module in Python makes writing data to CSV files very convenient. Simply follow the three steps above to complete the data writing operation. For other CSV file reading, parsing, and data processing operations, the csv module also provides a wealth of functions and options. Readers are welcome to explore on their own.