python csv文件转换为字节或类似对象的可查找文件

wribegjk  于 2021-08-20  发布在  Java
关注(0)|答案(1)|浏览(407)

背景:在aws lambda中使用python将csv文件发送到s3。
问题:无法让boto3接受我的csv文件或csv.reader对象。
例子:


# writing to csv file

with open('/tmp/' + output_file_name, 'a+') as csvfile:
    for row in csv_reader:
        # ... do data manipulation
        csv.DictWriter(csvfile, fieldnames=fields)

# read and send to s3

with open('/tmp/' + output_file_name, 'r') as file:
    s3_client = boto3.client('s3')
    s3_client.put_object(Body=file, Bucket='bucket-output', Key=output_file_name)

我收到了错误 TypeError: Unicode-objects must be encoded before hashing . 所以我试着打开文件用param读取 encoding='utf-8' 但是那里没有运气。。
boto3需要做什么才能“接受”csv文件?

vfh0ocws

vfh0ocws1#

我可以从本地驱动器读取csv并上传到s3

with open('test.csv', 'rb') as f:
    data = f.read().decode('utf-8')

boto3.client('s3').put_object(Body=data, Bucket=bucket, Key=key)

相关问题