reader读取csv文件,再用for循环遍历
import csv
with open('customer.csv')as f:
f_csv = csv.reader(f)
for row in f_csv:
print(row[0])
运行结果
[‘id’, ‘test’]
[‘932467’, ‘1111’]
[‘932468’, ‘2’]
[‘932469’, ‘3’]
[‘15896317’, ‘4’]
[‘15896319’, ‘5’]
[‘15896324’, ‘6’]
[‘15896321’, ‘7’]
如果需要第一列数据,则row[0]
import csv
with open('customer.csv')as f:
f_csv = csv.reader(f)
for row in f_csv:
print(row[0])
运行结果
id
932467
932468
932469
15896317
15896319
15896324
15896321
逐行获取csv文件的一列数据
import csv
bid_info = csv.DictReader(open('customer.csv','r'))
dict_data = []
for lines in bid_info:
if bid_info.line_num == 1:
continue
else:
dict_data.append(lines)
row_num = len(dict_data)
# print('this is all the data---' + str(dict))
#循环读取‘id’列的每一行
i = 0
while(i < row_num):
#print('this is'+str(i)+'row----'+ str(dict_data[i]))
customer = dict_data[i]['id']
print(customer)
i += 1
运行结果
932467
932468
932469
15896317
15896319
15896324
15896321