簡體   English   中英

Python CSV 文件到字節或可查找的類文件對象

[英]Python CSV file to bytes or seekable file-like object

背景:在 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 文件?

這適用於我從本地驅動器讀取 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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM