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.
line = data.read_line() cnt = 1 while line: print("Line {}: {}".format(cnt, line.strip())) line = data.readline() cnt += 1
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.
AttributeError: 'bytes' object has no attribute 'read_line'
How do I convert this to a file format I can read in, in a traditional python way? This generates the error:
with input_folder.get_download_stream("config.txt") as f: data = f.read()
Answers
-
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_stream
Thanks,
Mike -
Hi,
This is useful thanks!!!
Regards
i