简体   繁体   中英

How can I read .txt file from S3 bucket using python and view the contents?

data = s3client.get_object(Bucket='', Key='')
data1 = data['Body']

data2 = json.load(data1)
data2 = json_normalize(data=data2)
data2 = sor.explode('header.head')
data2 = data2.to_dict('records')
data2 = json_normalize(data2)

I am using the above code for json files to unnest complex jsons and analyse data. How can I do the same to stream text files and view the contents.

data = s3client.get_object(Bucket='', Key='')
data1 = data['Body']
with open(data1) as sample: 
    for line in sample: 
        print(line.rstrip()) 

I am getting the following error

TypeError: expected str, bytes or os.PathLike object, not StreamingBody

Generally, you would use iter_lines or iter_chunks . For example:

data = s3client.get_object(Bucket='', Key='')

for line in data['Body'].iter_lines(): 
    print(line) 

Note that line will be bytes , not str .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM