简体   繁体   中英

Python 3: convert a ascii char to unicode escaped

I need to convert a ascii char to unicode escaped

Example: "&" to "\\\&"

Context:

I receive in my input two values, first is a string and another is a raw byte with some contents. after this, the first string is used in the regex to catch data in raw.

teste = "teste's teste & teste" raw = '.... teste\\'s teste \\\& teste",null,["here","here2"] ....'

after this, regex is used with first var teste to get words here and here2 in var raw , but in the case when some chars like & have in the first var, he can't find in raw any pattern because in raw the var is in unicode escape.

so i try to convert some chars like & to unicode escape without success

Many thanks, I'll temporally solve this with:

def escape_word(word):
    whitelist = [" ", "'"] + list(string.ascii_letters)
    new_word = ""
    for _c in word:
        if _c in whitelist:
            new_word += _c
        else:
            new_word += "\\u%04x" % ord(_c)
    return new_word

until I find a better solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM