我想从给定字典(python)中的字符串中删除\ u00a9,\ u201d和类似的字符.

Man*_*mar 1 python string dictionary

d = {
    "key": "Impress the playing crowd with these classic "
           "Playing Cards \u00a9 Personalized Coasters.These beautiful"
           " coasters are made from glass, and measure approximately 4\u201d x 4\u201d (inches)"
           ".Great to look at, and lovely to the touch.There are 4 coasters in a set.We have "
           "created this exclusive design for all card lovers.Each coaster is a different suit, "
           "with the underneath.Make your next Bridge, or Teen Patti session uber-personal!"
           "Will look great on the bar, or any tabletop.Gift Designed for: Couples, Him, "
           "HerOccasion:Diwali, Bridge, Anniversary, Birthday"}
Run Code Online (Sandbox Code Playgroud)

我已经尝试了替换功能,但没有工作.

s = d[key].replace('\u00a9','')
Run Code Online (Sandbox Code Playgroud)

小智 10

如果要从字符串中删除所有Unicode字符,可以使用string.encode("ascii", "ignore").

它尝试将字符串编码为ASCII,第二个参数ignore告诉它忽略它无法转换的字符(所有Unicode字符)而不是抛出异常,因为它通常没有第二个参数,所以它返回一个字符串只有可以成功转换的字符,从而删除所有Unicode字符.

用法示例:

unicodeString = "Héllò StàckOvèrflow"
print(unicodeString.encode("ascii", "ignore")) # prints 'Hll StckOvrflow'
Run Code Online (Sandbox Code Playgroud)

更多信息:Python文档中的Unicodestr.encode()Unicode.