Crazywolf
Crazywolf

Reputation: 41

Reading a file line-wise

I have a file named data.dat in an S3 bucket, the contents of the file are in 3 lines

fruit
javaisnotafruit
xyq

I am reading from s3 this way

objectcontent = s3.get_object(Bucket="contents-bucket", Key=obj['Key'])
contentsins3 = objectcontent['Body'].read().decode('utf-8')
print(contentsins3)

When I do print(contentsins3) I am getting

fruit
javaisnotafruit
xyq

I have code for looping through the contents and read each line, how would I do that?

for i in contentsins3:
    print(i)

How would I get each line in S3 line by line?

Upvotes: 1

Views: 1125

Answers (2)

mootmoot
mootmoot

Reputation: 13166

This is just plain string parsing, regardless of the storage.

So back to python, the object contentsins3 contains the whole file data, together with the line break.

You can use splitlines() which will deal with \n, \r\n.

for i in contentsins3.splitlines():
    print(i)

Upvotes: 0

wim
wim

Reputation: 362497

The body is streaming, but when you do this you consume it all to EOF:

objectcontent['Body'].read()

So, don't do that. Instead:

import encodings
stream = encodings.utf_8.StreamReader(objectcontent['Body'])
for line in stream:
    ...

Upvotes: 2

Related Questions