user15330211
user15330211

Reputation:

read first five lines of a files from s3 using python

I tried to read s3 files using boto3

the code used is below

    content = s3.Object('bucket','objectkey').get()['Body'].read()
    StringVariable=content.decode('UTF-8','ignore')
    x = StringVariable.split('\n')
    str1 = ''.join(str(e) for e in x)
    print(str1)

it gets all the lines as bytes so i used decode and stored as string.

the output needed is only the first 5 lines to be read and stored as string.

thanks in advance.

Upvotes: 2

Views: 906

Answers (1)

Linux Geek
Linux Geek

Reputation: 967

Replace this:

 str1 = ''.join(str(e) for e in x)
 print(str1)

with this:

  for i in range(5):
        print(x[i])

It will only print the first five lines as you wanted it.

Upvotes: 1

Related Questions