【发布时间】:2016-10-08 18:55:41
【问题描述】:
我在 S3 上保存了一个文本文件,这是一个制表符分隔的表格。我想将它加载到熊猫中,但不能先保存它,因为我在 Heroku 服务器上运行。这是我目前所拥有的。
import io
import boto3
import os
import pandas as pd
os.environ["AWS_ACCESS_KEY_ID"] = "xxxxxxxx"
os.environ["AWS_SECRET_ACCESS_KEY"] = "xxxxxxxx"
s3_client = boto3.client('s3')
response = s3_client.get_object(Bucket="my_bucket",Key="filename.txt")
file = response["Body"]
pd.read_csv(file, header=14, delimiter="\t", low_memory=False)
错误是
OSError: Expected file path name or file-like object, got <class 'bytes'> type
如何将响应正文转换为 pandas 可以接受的格式?
pd.read_csv(io.StringIO(file), header=14, delimiter="\t", low_memory=False)
returns
TypeError: initial_value must be str or None, not StreamingBody
pd.read_csv(io.BytesIO(file), header=14, delimiter="\t", low_memory=False)
returns
TypeError: 'StreamingBody' does not support the buffer interface
更新 - 使用以下工作
file = response["Body"].read()
和
pd.read_csv(io.BytesIO(file), header=14, delimiter="\t", low_memory=False)
【问题讨论】:
-
试试这个方法:
io.BytesIO(file)或io.StringIO(file)在read_csv()调用中代替file -
你可以像this answer一样使用
io.StringIO。 -
这些建议都不起作用。您可以在我的帖子编辑中看到错误。
-
更新部分对我有用。谢谢。
标签: python pandas heroku amazon-s3 boto3