Remove all hex characters from string in Python

后端 未结 4 1121
失恋的感觉
失恋的感觉 2021-02-04 11:18

Although there are similar questions, I can\'t seem to find a working solution for my case:

I\'m encountering some annoying hex chars in strings, e.g.

\'         


        
4条回答
  •  灰色年华
    2021-02-04 11:46

    You could make it check for valid letters, and instead of typing out everything, it's possible to use the string module. The ones that may be useful to you are string.ascii_letters (contains both string.ascii_lowercase and string.ascii_uppercase), string.digits, string.printable and string.punctuation.

    I'd try string.printable first, but if it lets a few too many characters through, you could use a mix of the others.

    Here's an example of how I'd do it:

    import string
    valid_characters = string.printable
    start_string = '\xe2\x80\x9chttp://www.google.com\xe2\x80\x9d blah blah#%#@$^blah'
    end_string = ''.join(i for i in start_string if i in valid_characters)
    

提交回复
热议问题