Skip to content
Advertisement

How to get rid of b’ , all these x00, x** like things in bunch of strings in python 3.6?

I have strings as below:

JavaScript

I want to get rid of all these b’ and anything with x with 2 trailings like xe2, x80 and so on. I dont know how to get rid of it. I tried

JavaScript

But because content is already str, I can’t decode. So I tried below to make it like bytes and get rid of the things I want to get rid of and back to string but it does not work at all.

JavaScript

when I run this code below, I can get rid of ‘b and x** things so I tried every possible thing but I do not know how to make my strings to bytes one like below. I can convert content to bytes, but it doesnt get rid of the stuff.

JavaScript

Do you have any idea how my ‘content’ can get rid of b’ and all of x**?

Advertisement

Answer

You have a str value that contains the string representation of a bytes value, which itself is a UTF-8-encoded string. Use ast.literal_eval to get the actual bytes value, then decode it.

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement