Convert bytes to ascii and back save in Python?
13,686
Solution 1
OK I found a solution which is much easier than I thought
mybytes = 'ëýđþé'.encode()
str_mybytes = str(mybytes)
again_mybytes = eval(str_mybytes)
decoded = again_mybytes.decode('utf8')
Solution 2
x = x.decode().encode('ascii',errors='ignore')
Related videos on Youtube
Author by
Thagor
Updated on June 04, 2022Comments
-
Thagor almost 2 years
I'm Using python 3.5
I have a couple of byte strings representing text that is encoded in various codecs so:
b'mybytesstring'
, now some areUtf8
encoded other arelatin1
and so on. What I want to in the following order is:- transform the bytes string into an ascii like string.
- transform the ascii like string back to a bytes string.
- decode the bytes string with correct codec.
The problem is that I have to move the bytes string into something that does not accept bytes objects so I'm looking for a solution that lets me do
bytes -> ascii -> bytes
safely.-
juanpa.arrivillaga over 6 yearsDo you mean you have different
bytes
objects, each encoded differently? I don't understand what you want to dobytes --ascii--> str --???--> bytes
? -
Thagor over 6 yearsyes I have a group of
bytes
objects all have different encodings. I need to put all of them into a container that does not acceptbytes
objects onlystring
objects. Then I get them out of the container and I can decode them properly with the correct encoding.
-
import random over 6 yearsif you don't know the encoding, you can try using chardet to guess.
-
thejinx0r over 4 yearsWhile using
eval
does work, it felts a little unsafe for what I wanted to do. I found an answer that avoids the eval: stackoverflow.com/a/51775154/701403