python 3 - reading a file within zipped archive places 'b' character at start of each line -
in code below, strange output places b before every line. letter b.
e.g. sample output this:
[b'2017-06-01,15:19:57,'] the script this:
from zipfile import zipfile zipfile('myarchive.zip','r') myzip: myzip.open('logs/logfile1.txt') myfile: next(myfile) print(myfile.readlines()) the archive has single folder in called "logs" , inside logs there several text files, each lines below empty first line (hence next(myfile)
it places b before data, no matter file try read. if there multiple lines in file outputs this:
[b'2017-06-01,15:06:28,start session: \n', b'2017-06-01,15:06:36,stop session'] why placing pesky b there?
in python 3.x there distinction between strings , bytes data. when representing bytes strings python adds b prefix denote that. if want treat bytes strings, first need decode them string:
your_string = your_bytes.decode("utf-8") of course, codec you'll use depends on how string encoded bytes in first place.
Comments
Post a Comment