【问题标题】:How to remove all unicode representations in python如何删除python中的所有unicode表示
【发布时间】:2021-01-12 20:39:55
【问题描述】:

我正在尝试删除文档中特殊字符的所有表示形式,例如文档的一部分说:“world\u2019s”,当我拆分它时,它给出了['world', '\u2019', 's'],但我只需要单词(unicode 和 ' s' 删除)。
我已经删除了所有标点符号,这适用于通常不在这些 un​​icode 表示中显示的实际标点符号。 而且我还尝试使用正则表达式来匹配以“\”开头的所有内容,但这似乎也不起作用。

【问题讨论】:

    标签: python unicode ascii


    【解决方案1】:
    import re
    
    string = "world\u2019s"
    
    print (re.sub(r"\b([^\s]+)\\([^\s]+)\b",r'\1',str(string.encode('ascii', 'backslashreplace'), 'ascii')))
    

    输出:

    world
    

    您可以将其应用于整个字符串文档,应该可以正常工作。

    import re
    
    string = "world\u2019s h\u2018e"
    
    print (re.sub(r"\b([^\s]+)\\([^\s]+)\b",r'\1',str(string.encode('ascii', 'backslashreplace'), 'ascii')))
    

    输出:

    world h
    

    【讨论】:

    • 并非在所有情况下都有效:string = "world\u2019s h\u2018e" => world\u2019s h :)
    猜你喜欢
    • 2020-03-28
    • 2017-05-22
    • 1970-01-01
    • 2016-03-26
    • 2011-01-12
    • 2017-06-15
    • 1970-01-01
    • 1970-01-01
    • 2011-03-10
    相关资源
    最近更新 更多