CSV处理:在每次迭代中从标题开始遍历行

2024-09-28 19:32:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要循环浏览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=? 
...

Tags: csvthegettopetcdosomethingreader
2条回答

根据你最新的问题,这更好吗

import csv

fileName = 'csv_file_test.csv'
with open(fileName, 'r', encoding='UTF-8', newline='') as csvfile:
    reader_d = csv.DictReader(csvfile)

    for num, row in enumerate(reader_d, 1):
        data = ', '.join(f'{name}={value}' for name, value in row.items())
        print(f'for the row#{num}: {data}')

打印输出:

for the row#1: field1=11, field2=12, field3=13
for the row#2: field1=21, field2=22, field3=23

像熊猫这样的东西也许能帮你。你可以这样做

import pandas as pd

df = pd.read_csv(your_csv_file_here)

print(df[col_X])

指向read_csv-https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_csv.html文档的链接

相关问题 更多 >