Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python how to remove escape characters from a string

Tags:

python

I have a string like below, and I want to remove all \x06 characters from the string in Python.

Ex:

s = 'test\x06\x06\x06\x06'
s1 = 'test2\x04\x04\x04\x04'
print(literal_eval("'%s'" % s))

output: test♠♠♠♠

I just need String test and remove all \xXX.

like image 854
ashkus Avatar asked Mar 01 '26 08:03

ashkus


1 Answers

Maybe the regex module is the way to go

>>> s = 'test\x06\x06\x06\x06'
>>> s1 = 'test2\x04\x04\x04\x04'
>>> import re
>>> re.sub('[^A-Za-z0-9]+', '', s)
'test'
>>> re.sub('[^A-Za-z0-9]+', '', s1)
'test2'
like image 187
chickity china chinese chicken Avatar answered Mar 03 '26 21:03

chickity china chinese chicken



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!