温馨提示×

python如何遍历csv文件

冬季
2836
2021-05-27 19:43:36
栏目: 编程语言

使用python遍历csv文件的方法:1.新建python项目;2.导入pandas模块;3.使用read_csv()函数打开文件;4.使用iterrows()方法遍历文件;

python如何遍历csv文件

具体步骤如下:

1.首先,打开python,并新建一个python项目;

2.python项目创建好后,在项目中使用import方法导入pandas模块;

import pandas as pd

3.pandas模块导入后,使用read_csv()函数打开需要遍历的csv文件;

file = pd.read_csv('text.csv')

4.最后,csv文件打开后,在for循环中使用iterrows()方法即可对csv文件进行遍历;

for index, row in file.iterrows():

x, y = row['X'], row['Y']

0