使用Boto读取Pandas中的文件(文件名部分已知)

问题描述 投票:0回答:1

我需要从S3读取一个csv文件(使用boto)以创建pandas数据帧。问题是文件名是我知道的部分。我可以使用glob和pd_read csv从我的系统中读取文件(我知道文件的部分名称)。

如何使用Boto完成此操作?

文件名是'CELLBH_testing_phase1_automated_1234xvy345.csv',我只知道CELLBH是已知的关键字。休息串不断变化。

使用boto读取文件的代码,我知道确切的文件名:

access_key="xxxxxxxxxx"
secret_key="xxxxxxxxxx"

conn=boto.connect_s3(
    aws_access_key_id=access_key,
    aws_secret_access_key=secret_key,
    host='xxxxxxxxx',
    is_secure=False,
    calling_format=boto.s3.connection.OrdinaryCallingFormat(),
    )
bucket=conn.get_bucket('npousecase',validate=False)

Test_File='CELLBH.csv'
k=Key(bucket,Test_File)
content=k.get_contents_as_string()
Test=pd.read_csv(StringIO.StringIO(content),sep=";",header=0)

用于读取文件'CELLBH_testing_phase1_automated_1234xvy345.csv'的代码,如果它在我的系统上

data_dir="C:\\users\\adbharga\\Desktop\\Input"
os.chdir(data_dir)

## Reading files from Input Directory

for f in glob.glob('CELLBH*.csv'):
    Test = pd.read_csv(f,sep=";",header=0)

我如何使用Boto完成上述操作?希望问题很清楚。谢谢

python-3.x pandas boto
1个回答
0
投票

检查这个答案:How to read a csv file from an s3 bucket using Pandas in Python似乎你可以围绕答案代码循环来获得你想要的东西。

喜欢:

for bucket_name in glob.glob('CELLBH*.csv'):

     object_key = 'my_file.csv'
     csv_obj = client.get_object(Bucket=bucket_name, Key=object_key)
     body = csv_obj['Body']
     csv_string = body.read().decode('utf-8')
     df = pd.read_csv(StringIO(csv_string))
© www.soinside.com 2019 - 2024. All rights reserved.