Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • 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.

like image 609
Thagor Avatar asked Sep 14 '25 23:09

Thagor


1 Answers

x = x.decode().encode('ascii',errors='ignore')
like image 78
DYZ Avatar answered Sep 17 '25 13:09

DYZ