我正在使用boto
来读取csv
文件并解析它的内容。这是我写的代码:
import boto
from boto.s3.key import Key
import pandas as pd
import io
conn = boto.connect_s3(keyId, sKeyId)
bucket = conn.get_bucket(bucketName)
# Get the Key object of the given key, in the bucket
k = Key(bucket, srcFileName)
content = k.get_contents_as_string()
reader = pd.read_csv(io.StringIO(content))
for row in reader:
print(row)
但是我在read_csv行收到错误:
TypeError: initial_value must be str or None, not bytes
如何解决此错误并解析S3
更新:如果我使用BytesIO
代替StringIO
,那么print(row)
行只会打印csv的第一行。我该如何循环呢?
这是我目前的代码:
import boto3
s3 = boto3.resource('s3',aws_access_key_id = keyId, aws_secret_access_key = sKeyId)
obj = s3.Object(bucketName, srcFileName)
content = obj.get_contents_as_string()
reader = pd.read_csv(io.BytesIO(content), header=None)
count = 0
for index, row in reader.iterrows():
print(row[1])
当我执行此操作时,我收到AttributeError: 's3.Object' object has no attribute 'get_contents_as_string'
错误