Thagor
Thagor

Reputation: 900

Convert bytes to ascii and back save in Python?

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:

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.

Upvotes: 4

Views: 22420

Answers (3)

Thagor
Thagor

Reputation: 900

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

Upvotes: 0

Irmen de Jong
Irmen de Jong

Reputation: 2847

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.

Upvotes: 0

DYZ
DYZ

Reputation: 57033

x = x.decode().encode('ascii',errors='ignore')

Upvotes: 4

Related Questions