Submit your inspiring success story or innovative use case to the 2022 Dataiku Frontrunner Awards! ENTER YOUR SUBMISSION

Read a file line by line, in Python when read in as bytes

indy2005
Level 3
Read a file line by line, in Python when read in as bytes

Hi,

 

I am trying to read in a config.txt file stored in a local managed folder.

 

 

with input_folder.get_download_stream("config.txt") as f:
     data = f.read()

 

 

However when I try and iterate over the text file to read it in, I get an error as it is a bytes file and python doesnt see it as a file.

 

 

line = data.read_line()
cnt = 1
while line:
    print("Line {}: {}".format(cnt, line.strip()))
    line = data.readline()
    cnt += 1

 

 

How do I convert this to a file format I can read in, in a traditional python way?  This generates the error:

 

AttributeError: 'bytes' object has no attribute 'read_line'

 

0 Kudos
2 Replies
MikeG
Dataiker
Dataiker

Hi @indy2005 

Here's a quick example. Please let me know if you have follow up questions or if I have misunderstood your question.

I have a file called `config.txt` in a managed folder with id `kiOwqj8S`.

Contents of config.txt:

cat config.txt
config_a = 1
config_b = 2
config_c = 3


In a Python notebook in DSS I run the following code:

import dataiku

folder = dataiku.Folder("kiOwqj8S", project_key="COMMUNITY_25416")

with folder.get_download_stream("/config.txt") as stream:
    Lines = stream.readlines()
    count = 1
    for line in Lines:
        print("Line{}: {}".format(count, line.strip()))
        count += 1

output:

Line1: b'config_a = 1'
Line2: b'config_b = 2'
Line3: b'config_c = 3'

Including link to documentation in case helpful: https://doc.dataiku.com/dss/latest/python-api/managed_folders.html#dataiku.Folder.get_download_strea...

 

Thanks,
Mike

indy2005
Level 3
Author

Hi,

 

This is useful thanks!!!

 

Regards

 

i

0 Kudos