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')
Share:
13,686

Related videos on Youtube

Thagor
Author by

Thagor

Updated on June 04, 2022

Comments

  • Thagor
    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 are Utf8 encoded other are latin1 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
      juanpa.arrivillaga over 6 years
      Do you mean you have different bytes objects, each encoded differently? I don't understand what you want to do bytes --ascii--> str --???--> bytes?
    • Thagor
      Thagor over 6 years
      yes I have a group of bytes objects all have different encodings. I need to put all of them into a container that does not accept bytes objects only string objects. Then I get them out of the container and I can decode them properly with the correct encoding.
  • import random
    import random over 6 years
    if you don't know the encoding, you can try using chardet to guess.
  • thejinx0r
    thejinx0r over 4 years
    While 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