string.translate() with unicode data in python

后端 未结 5 480
情书的邮戳
情书的邮戳 2020-12-03 05:05

I have 3 API\'s that return json data to 3 dictionary variables. I am taking some of the values from the dictionary to process them. I read the specific values that I want t

5条回答
  •  Happy的楠姐
    2020-12-03 05:36

    As I stumbled upon the same problem and Simon's answer was the one that helped me to solve my case, I thought of showing an easier example just for clarification:

    from collections import defaultdict
    

    And then for the translation, say you'd like to remove '@' and '\r' characters:

    remove_chars_map = defaultdict()
    remove_chars_map['@'] = None
    remove_chars_map['\r'] = None
    
    new_string = old_string.translate(remove_chars_map)
    

    And an example:

    old_string = "word1@\r word2@\r word3@\r"

    new_string = "word1 word2 word3"

    '@' and '\r' removed

提交回复
热议问题