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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With