4

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.

2
  • Do you mean you have different bytes objects, each encoded differently? I don't understand what you want to do bytes --ascii--> str --???--> bytes? Commented Sep 6, 2017 at 23:04
  • 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. Commented Sep 7, 2017 at 8:07

3 Answers 3

4
x = x.decode().encode('ascii',errors='ignore')
Sign up to request clarification or add additional context in comments.

Comments

0

You use the encode and decode methods for this, and supply the desired encoding to them. It's not clear to me if you know the encoding beforehand. If you don't know it you're in trouble. You may have to guess the encoding in some way, risking garbage output.

1 Comment

if you don't know the encoding, you can try using chardet to guess.
0

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')

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.