乐闻世界logo
搜索文章和话题

How to parse CSV data with Python?

1个答案

1

When parsing CSV (Comma-Separated Values) data, we typically follow the following steps:

1. Read the File

First, we need to read the file that contains the CSV data. This can be done using the open function from Python's standard library, as shown below:

python
file = open('filename.csv', 'r')

2. Use the CSV Module

Python's standard library includes a csv module, which provides functions for reading and writing CSV files. Using this module, we can create a CSV reader that reads the file line by line and automatically handles commas and quotes in the data.

python
import csv reader = csv.reader(file)

3. Iterate Over the Data

By iterating over the CSV reader, we can process the data line by line. Each line is returned as a list, with each element representing a column.

python
for row in reader: print(row)

4. Process the Data

As we read each line, we can process the data, for example, by converting data types, filtering records, or performing calculations.

For instance, if we want to convert the price column (assuming it is the third column) from string to float and calculate the total price of all products:

python
total_price = 0 for row in reader: price = float(row[2]) # Convert string to float total_price += price print(f"Total Price: {total_price}")

5. Close the File

Finally, remember to close the file to free up system resources.

python
file.close()

Example

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

shell
name,category,price apple,fruit,0.5 banana,fruit,0.3

We can use the following code to calculate the total price of all products:

python
import csv with open('products.csv', 'r') as file: reader = csv.reader(file) next(reader) # Skip the header total_price = 0 for row in reader: price = float(row[2]) total_price += price print(f"Total Price: {total_price}")

Here, we use the with statement to automatically manage file opening and closing, and next(reader) to skip the header row.

This outlines the basic steps for parsing CSV files and provides a simple example. Using Python's csv module, we can efficiently read and process CSV data.

2024年6月29日 12:07 回复

你的答案