JavaEar 专注于收集分享传播有价值的技术资料

How to remove escape characters from string in python?

I have string that look like this text = u'\xd7\nRecord has been added successfully, record id: 92'. I tried to remove the escape character \xd7 and \n from my string so that I could use it for another purpose.

I tried str(text). It works but it could not remove character \xd7.

UnicodeEncodeError: 'ascii' codec can't encode character u'\xd7' in position 0: ordinal not in range(128)

Any way I could do to remove any escape character as such above from string? Thanks

4个回答

    最佳答案
  1. You can try the following using replace :

    text=u'\xd7\nRecord has been added successfully, record id: 92'
    bad_chars = ['\xd7', '\n', '\x99m', "\xf0"] 
    for i in bad_chars : 
        text = text.replace(i, '') 
    text
    
  2. 参考答案2
  3. I believe Regex can help

    import re
    text = u'\xd7\nRecord has been added successfully, record id: 92'
    res = re.sub('[^A-Za-z0-9]+', ' ', text).strip()
    

    Result:

    'Record has been added successfully record id 92'
    
  4. 参考答案3
  5. You could use the built-in regex library.

    import re
    text = u'\xd7\nRecord has been added successfully, record id: 92'
    result = re.sub('[^A-Za-z0-9]+', ' ', text)
    
    print(result)
    

    That spits out Record has been added successfully record id 92

    This seems to pass your test case if you can live without the punctuation.

  6. 参考答案4
  7. Try regex.

    
    import re
    def escape_ansi(line):
        ansi_escape =re.compile(r'(\xd7|\n)')
        return ansi_escape.sub('', line)
    
    text = u'\xd7\nRecord has been added successfully, record id: 92'
    print(escape_ansi(line))