python读取csv文件代码示例
时间: 2023-09-08 21:14:22 浏览: 91
:import csv with open('example.csv', 'r') as f: reader = csv.reader(f) for row in reader: print(row)
Hi there! Can you give me an example of how to read a CSV file in Python?
Sure! Here's a simple example of how to read a CSV file in Python:
```
import csv
with open('example.csv', 'r') as f:
reader = csv.reader(f)
for row in reader:
print(row)
```
This code uses the `csv` module to read the contents of a CSV file. The `open` function is used to open the file, and the `reader` object is created using the `csv.reader` function. The `for` loop then iterates over the rows in the file, and the `print` function is used to display each row.
相关问题
python读取csv文件的代码示例
使用Python读取CSV文件的示例代码如下:import csv
# 设定 CSV 文件的路径
csv_file = "example.csv"
# 打开并读取 CSV 文件
with open(csv_file, 'r') as file:
reader = csv.reader(file)
# 遍历 CSV 文件中的每一行
for row in reader:
print(row)
python读取csv文件多行代码示例
Here is an example of reading multiple lines from a CSV file in Python:
```
import csv
with open('data.csv', 'r') as file:
reader = csv.reader(file)
for row in reader:
print(row)
```
This code will read the `data.csv` file and print each row as a list of values. The `csv.reader` function is used to create a reader object that can be used to iterate over the rows in the file. The `with` statement is used to ensure that the file is properly closed after it has been read.