【问题标题】:Python encoding/decoding problemsPython编码/解码问题
【发布时间】:2015-03-15 19:04:53
【问题描述】:

如何将诸如“weren\xe2\x80\x99t”之类的字符串解码回正常编码。

所以这个词实际上是不是而不是“weren\xe2\x80\x99t”? 例如:

print "\xe2\x80\x9cThings"
string = "\xe2\x80\x9cThings"
print string.decode('utf-8')
print string.encode('ascii', 'ignore')

“Things
“Things
Things

但我实际上想得到“东西”。

或:

print "weren\xe2\x80\x99t"
string = "weren\xe2\x80\x99t"
print string.decode('utf-8')
print string.encode('ascii', 'ignore')

weren’t
weren’t
werent

但我实际上想得到的不是。

我该怎么做?

【问题讨论】:

  • 你需要提供你想要的翻译字典——例如从花哨的引号到纯 ASCII 的——并使用 Unicode 字符串的.translate 方法来应用它。我认为周围没有标准的“asciify it down”翻译词典......
  • 嗯,我刚做了一个:)

标签: python python-2.7 encoding ascii non-ascii-characters


【解决方案1】:

在 Python 3 中,我会这样做:

string = "\xe2\x80\x9cThings"
bytes_string = bytes(string, encoding="raw_unicode_escape")
happy_result = bytes_string.decode("utf-8", "strict")
print(happy_result)

不需要翻译地图,只需代码:)

【讨论】:

  • 我正在寻找这个答案!
【解决方案2】:

我映射了最常见的奇怪字符,因此这是基于 Oliver W. 答案的非常完整的答案。

这个功能并不理想,但它是最好的起点。 还有更多的字符定义:

http://utf8-chartable.de/unicode-utf8-table.pl?start=8192&number=128&utf8=string
http://www.utf8-chartable.de/unicode-utf8-table.pl?start=128&number=128&names=-&utf8=string-literal

...

def unicodetoascii(text):

    uni2ascii = {
            ord('\xe2\x80\x99'.decode('utf-8')): ord("'"),
            ord('\xe2\x80\x9c'.decode('utf-8')): ord('"'),
            ord('\xe2\x80\x9d'.decode('utf-8')): ord('"'),
            ord('\xe2\x80\x9e'.decode('utf-8')): ord('"'),
            ord('\xe2\x80\x9f'.decode('utf-8')): ord('"'),
            ord('\xc3\xa9'.decode('utf-8')): ord('e'),
            ord('\xe2\x80\x9c'.decode('utf-8')): ord('"'),
            ord('\xe2\x80\x93'.decode('utf-8')): ord('-'),
            ord('\xe2\x80\x92'.decode('utf-8')): ord('-'),
            ord('\xe2\x80\x94'.decode('utf-8')): ord('-'),
            ord('\xe2\x80\x94'.decode('utf-8')): ord('-'),
            ord('\xe2\x80\x98'.decode('utf-8')): ord("'"),
            ord('\xe2\x80\x9b'.decode('utf-8')): ord("'"),

            ord('\xe2\x80\x90'.decode('utf-8')): ord('-'),
            ord('\xe2\x80\x91'.decode('utf-8')): ord('-'),

            ord('\xe2\x80\xb2'.decode('utf-8')): ord("'"),
            ord('\xe2\x80\xb3'.decode('utf-8')): ord("'"),
            ord('\xe2\x80\xb4'.decode('utf-8')): ord("'"),
            ord('\xe2\x80\xb5'.decode('utf-8')): ord("'"),
            ord('\xe2\x80\xb6'.decode('utf-8')): ord("'"),
            ord('\xe2\x80\xb7'.decode('utf-8')): ord("'"),

            ord('\xe2\x81\xba'.decode('utf-8')): ord("+"),
            ord('\xe2\x81\xbb'.decode('utf-8')): ord("-"),
            ord('\xe2\x81\xbc'.decode('utf-8')): ord("="),
            ord('\xe2\x81\xbd'.decode('utf-8')): ord("("),
            ord('\xe2\x81\xbe'.decode('utf-8')): ord(")"),

                            }
    return text.decode('utf-8').translate(uni2ascii).encode('ascii')

print unicodetoascii("weren\xe2\x80\x99t")  

【讨论】:

    【解决方案3】:

    您应该提供一个将 unicode 字符映射到其他 unicode 字符的翻译映射(如果您想重新编码,后者应该在 ASCII 范围内):

    uni2ascii = {ord('\xe2\x80\x99'.decode('utf-8')): ord("'")}    
    yourstring.decode('utf-8').translate(uni2ascii).encode('ascii')
    print(yourstring)  # prints: "weren't"
    

    【讨论】:

    • 我知道我可以做到这一点。但是有没有现成的地图可以自动做到这一点?
    猜你喜欢
    • 1970-01-01
    • 2020-07-23
    • 2014-07-16
    • 1970-01-01
    • 2018-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多