3

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.

9
  • What about \x04 Commented Aug 9, 2018 at 5:56
  • 2
    Also, string or bytes? Py 2 or py 3? What have you tried? What research have you done? Commented Aug 9, 2018 at 5:58
  • 2
    Possible duplicate of How to remove all the escape sequences from a list of strings? Commented Aug 9, 2018 at 6:00
  • 1
    @davedwards I have tried this and it returns test♠♠♠♠. I just need string 'test' Commented Aug 9, 2018 at 6:04
  • 2
    @ashkus you're right, how about this: import re; re.sub('[^A-Za-z0-9]+', '', s) Commented Aug 9, 2018 at 6:18

3 Answers 3

6

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' 
Sign up to request clarification or add additional context in comments.

Comments

2

If you want to remove all \xXX characters (non-printable ascii characters) the best way is probably like so

import string def remove_non_printable(s): return ''.join(c for c in s if c not in string.printable) 

Note this won't work with any non-ascii printable characters (like é, which will be removed).

Comments

0

This should do it

import re #Import regular expressions s = 'test\x06\x06\x06\x06' #Input s s1 = 'test2\x04\x04\x04\x04' #Input s1 print(re.sub('\x06','',s)) #remove all \x06 from s print(re.sub('\x04','',s1)) #remove all \x04 from s1 

2 Comments

regular expressions are overkill for this method. Just use str.replace
i believe that in the second print statement is should be 's1', not s

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.