相关疑难解决方法(0)

将数据帧直接保存到csv到s3 Python

我有一个pandas DataFrame,我想上传到新的CSV文件.问题是我不想在将文件传输到s3之前将其保存在本地.是否有像to_csv这样的方法直接将数据帧写入s3?我正在使用boto3.
这是我到目前为止:

import boto3
s3 = boto3.client('s3', aws_access_key_id='key', aws_secret_access_key='secret_key')
read_file = s3.get_object(Bucket, Key)
df = pd.read_csv(read_file['Body'])

# Make alterations to DataFrame

# Then export DataFrame to CSV through direct transfer to s3
Run Code Online (Sandbox Code Playgroud)

python csv amazon-s3 dataframe boto3

87
推荐指数
8
解决办法
6万
查看次数

如何将AWS S3上的文本文件导入到pandas中而无需写入磁盘

我有一个文本文件保存在S3上,这是一个制表符分隔表.我想将它加载到pandas但不能保存它,因为我在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)
Run Code Online (Sandbox Code Playgroud)

错误是

OSError: Expected file path name or file-like object, got <class 'bytes'> type
Run Code Online (Sandbox Code Playgroud)

如何将响应体转换为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
Run Code Online (Sandbox Code Playgroud)

更新 - 使用以下工作

file = response["Body"].read()
Run Code Online (Sandbox Code Playgroud)

pd.read_csv(io.BytesIO(file), header=14, …
Run Code Online (Sandbox Code Playgroud)

python heroku amazon-s3 pandas boto3

68
推荐指数
5
解决办法
5万
查看次数

标签 统计

amazon-s3 ×2

boto3 ×2

python ×2

csv ×1

dataframe ×1

heroku ×1

pandas ×1