Python Get Rid of Bytes B' '

Getting rid of the 'b' in front of Python strings

You have to decode x into bytes and append the returning value to the fixedtimeslist list.

for x in timeslist:
fixedtimeslist.append(x.decode('utf-8'))

How do I remove bytestrings left over from decompression from a string?

I am guessing that you mistakenly assigned the above byte string “sentence” to an object of type str. Instead, it needs to be assigned to a byte string object and interpret it as a sequence of UTF-8 bytes. Compare:

b = b'... known as \xe2\x80\x9ccomorbidity\xe2\x80\x9d and ...'
s = b.decode('utf-8')
print(b)
# b'... known as \xe2\x80\x9ccomorbidity\xe2\x80\x9d and ...'
print(s)
# ... known as “comorbidity” and ...

Either way, the issue is unrelated to compression: a lossless compression (such as bzip2) roundtrip never changes the data:

print(bz2.decompress(bz2.compress(b)).decode('utf-8'))
# ... known as “comorbidity” and ...


Related Topics



Leave a reply



Submit