0

In python, I am using a binary file to store data, but it does store a bit of text. I'm struggling to find a way to convert bytes to string for when reading from the file. I would (for example) like to change b'Initial setup.' to the form "Initial setup.".

I have tried:

>>> str(chr(i)for i in b"Initial setup.")

But that just returns:

'<generator object <genexpr> at 0x03B81CC0>'

I also tried:

>>> sum(chr(i)for i in b"Initial setup.")

But it seems sum only works for numbers.

In case it's useful, the reverse effect can be achieved using:

>>> bytes([ord(i)for i in "Initial setup."])
b'Initial setup.'

What would be the best ways to convert bytes to str and what are the advantages of each method?

Programmer S
  • 429
  • 7
  • 21
  • 1
    Just `decode` it. If you know your strings are ASCII or Latin-1, `b.decode(‘ascii')` or `b.decode('latin1')` will be much faster than any loop you can write in pure Python (at least in CPython). If they’re in some other encoding like UTF-8, `b.decode('utf8')` has the added advantage of actually being correct, while copying bytes to code points directly is wrong. – abarnert May 31 '18 at 17:38
  • Also, since you’re asking about efficiency: the reason `sum` doesn’t work for strings is efficiency. String concatenation takes worst-case linear time, so concatenating N strings together with `sum` could take quadratic time. That’s exactly what the `join` method is there for—and IIRC, in current versions of Python, the error message even tells you to use `''.join`. – abarnert May 31 '18 at 17:40

0 Answers0