python pandas csv 存储_如何在Python中使用Pandas从s3存储桶读取csv文件

博客介绍了使用Python默认的csv库读取AWS上CSV文件的方法。通过建立连接、获取文件键、读取内容并存储为字符串,再进行分割处理,最终将数据存储在列表中返回,为处理CSV文件提供了一种不依赖pandas的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

你不需要熊猫。。您可以只使用python的默认csv库def read_file(bucket_name,region, remote_file_name, aws_access_key_id, aws_secret_access_key):

# reads a csv from AWS

# first you stablish connection with your passwords and region id

conn = boto.s3.connect_to_region(

region,

aws_access_key_id=aws_access_key_id,

aws_secret_access_key=aws_secret_access_key)

# next you obtain the key of the csv you want to read

# you will need the bucket name and the csv file name

bucket = conn.get_bucket(bucket_name, validate=False)

key = Key(bucket)

key.key = remote_file_name

data = key.get_contents_as_string()

key.close()

# you store it into a string, therefore you will need to split it

# usually the split characters are '\r\n' if not just read the file normally

# and find out what they are

reader = csv.reader(data.split('\r\n'))

data = []

header = next(reader)

for row in reader:

data.append(row)

return data

希望它能解决你的问题,

祝你好运!

:)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值