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

Viewed 1578
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
1 Answers

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.

Related