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?