我试图遍历.csv文件,以便可以读取每一行,但收到以下错误:
AttributeError: '_io.TextIOWrapper' object has no attribute 'iterrows'
到目前为止,这是我的代码:
def i_list(input_file):
input_list = []
file = open(input_file, 'r')
for i, j in file.iterrows():
num1 = j[1]
num2 = j[2]
input_list.append(num1)
input_list.append(num2)
i_list('nums.csv')
答案 0 :(得分:2)
您可以使用csv library
import reader from csv
input_list=[]
f = reader(open(input_file))
header = next(f) # if there is a header
for num1, num2 in f:
input_list.append(num1)
input_list.append(num2)
答案 1 :(得分:1)
您可以使用Pandas:
import pandas as pd
df = pd.read_csv('file.csv')
for row in df.iterrows():
# do something
如果您不想使用熊猫,请尝试使用file.readlines()
with open(filepath, 'r') as f:
lines = f.readlines()
for line in lines:
# do something