python的列表和CSV阅读器

2024-09-28 17:22:01 发布

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

我有一个小的CSV文件:

name,dept,city
sri,cse,hyd
vatsasa,ece,bang

我可以通过csvreader读取csv文件,如下所示:

response = s3.get_object(Bucket=src_bucket, Key=key)
lines = response['Body'].read().splitlines(True)
reader = csv.reader(lines)
first_row = next(reader)
print(' first row is: ', first_row)
readers=list(reader)
for row1 in readers:
    print('this is second iteration: ', row1)

以下是上述代码的结果:

first row is: ['name', 'Dept', 'City']
this is second iteration: ['sree', 'NULL', 'Bengaluru']
this is second iteration: ['vatsasa', 'NULL', 'Hyd']
this is second iteration: ['NULL', 'NULL', 'VJA']
this is second iteration: ['capgemini', 'NULL', 'TPTY']
this is second iteration: ['DTP', 'NULL', 'NULL']
this is second iteration: ['Bengaluru', 'NULL', 'TVM']
this is second iteration: ['sre', 'NULL', 'MNGL']
this is second iteration: ['vatsas', 'NULL', 'Kochi']
this is second iteration: ['NULL', 'NULL', 'TVM']
this is second iteration: ['capgemin', 'NULL', 'MNGL']
this is second iteration: ['DTP9', 'NULL', 'Kochi']
this is second iteration: ['NULL', 'NULL', 'TVM']
this is second iteration: ['sree0', 'NULL', 'MNGL']

不过,我曾尝试在脚本末尾将reader中的行打印为:

response = s3.get_object(Bucket=src_bucket, Key=key)
lines = response['Body'].read().splitlines(True)
reader = csv.reader(lines)
first_row = next(reader)
print(' first row is: ', first_row)
readers=list(reader)
for row1 in readers:
print('this is second iteration: ', row1)
for row in reader:
    print('this is first iteration: ', row)

但结果和上面一样:

first row is: ['name', 'Dept', 'City']
this is second iteration: ['sree', 'NULL', 'Bengaluru']
this is second iteration: ['vatsasa', 'NULL', 'Hyd']
this is second iteration: ['NULL', 'NULL', 'VJA']
this is second iteration: ['capgemini', 'NULL', 'TPTY']
this is second iteration: ['DTP', 'NULL', 'NULL']
this is second iteration: ['Bengaluru', 'NULL', 'TVM']
this is second iteration: ['sre', 'NULL', 'MNGL']
this is second iteration: ['vatsas', 'NULL', 'Kochi']
this is second iteration: ['NULL', 'NULL', 'TVM']
this is second iteration: ['capgemin', 'NULL', 'MNGL']
this is second iteration: ['DTP9', 'NULL', 'Kochi']
this is second iteration: ['NULL', 'NULL', 'TVM']
this is second iteration: ['sree0', 'NULL', 'MNGL']

来自第一次迭代的行,即来自读卡器的行不会被打印。你知道吗

我担心的是,我必须使用csv文件进行进一步的检查,但一开始无法从“reader”读取行。你知道吗

确认时,是否应使用“读卡器”进行进一步处理或需要从“读卡器”处读取?你知道吗

注意:在aws lambda上尝试了此代码


Tags: csvisresponsethisnullreaderrowfirst