python

时间:2019-03-06 04:10:16

标签: python amazon-web-services aws-lambda

我有一个小的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']

但是,我曾尝试在脚本结尾处从阅读器打印行:

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文件进行进一步的检查,但首先无法从“读取器”读取行。

为进行确认,使用“阅读器”进行进一步处理还是需要从“阅读器”中阅读是否合适?

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

1 个答案:

答案 0 :(得分:0)

您的代码行中有一个错误

for row in reader:
    print('this is first iteration: ', row)

使用阅读器,而不是 阅读器