我需要循环浏览CSV行,如下所示:
top row, row2
top row, row3
...etc
更具体地说:
Loop starts:
First iteration:
Get the top row#1 (header)
Do something, extractions etc
Get the row#2
Do something, extractions etc
Second iteration:
Get the top row#1 (header)
Do something, extractions etc
Get the row#3
Do something, extractions etc
Third iteration:
Get the top row#1 (header)
Do something, extractions etc
Get the row#4
Do something, extractions etc
...etc...
Loop finishes
我的想法是(也许有更好的想法):
输入CSV:
field1,field2,field3
11,12,13
21,22,23
import csv
fileName = 'csv_file_test.csv'
with open(fileName, 'r', encoding='UTF-8') as csvfile:
reader_d = csv.DictReader(csvfile)
header_d = next(reader_d)
print("header_d: ")
print(header_d)
for row in reader_d:
print(row)
结果还不错,我只需要帮助从这个目录中提取(迭代)每个元素,请:
header_d:
OrderedDict([('field1', '11'), ('field2', '12'), ('field3', '13')])
OrderedDict([('field1', '21'), ('field2', '22'), ('field3', '23')])
我不知道有多少列,所以我必须在每次迭代中从第2行开始遍历每一行的每一列。所以我基本上需要每个行的列名称和列值,例如:
我需要找到每行的列名及其对应值:
for the row#2: column name=? and value=?
for the row#3: column name=? and value=?
...
根据你最新的问题,这更好吗
打印输出:
像熊猫这样的东西也许能帮你。你可以这样做
指向read_csv-https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_csv.html文档的链接
相关问题 更多 >
编程相关推荐