Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does read() output a byte and not a string?

I am trying to analyze some binary files and assumed that Python's read() function returned a string from this article and a Tutorials Point article.

Yet when I messed around read() myself I got a something other than what I read.

>>> with gzip.open('RTLog_20150424T194428.gz') as f:
       a = f.read(3)
       print(a)
       type(a)


b'use'
<class 'bytes'>
>>> a
b'use'
>>> str(a)
"b'use'"
>>> b = 'asdfasdfasdf'
>>> type(b)
<class 'str'>
>>> 

When tested on my own, the output of a read() call returned a <class 'bytes'> object, not a <class 'str'> object.

What am I not getting?

like image 323
Dzhao Avatar asked Dec 10 '22 17:12

Dzhao


1 Answers

You can open in rb or rt mode (the default is read binary, giving you bytes). This is mentioned in the gzip.open docstring:

The mode argument can be "r", "rb", "w", "wb", "x", "xb", "a" or "ab" for binary mode, or "rt", "wt", "xt" or "at" for text mode. The default mode is "rb", and the default compresslevel is 9.

If you pass the keyword argument mode="rt" when opening (and you know the right encoding), then you should get a string returned when calling read method.

like image 152
wim Avatar answered Dec 13 '22 07:12

wim