【问题标题】:Encoding issue for Python tool Unidecode on CLCL 上 Python 工具 Unidecode 的编码问题
【发布时间】:2014-02-25 22:18:54
【问题描述】:

我需要将 unicode 文件转换为 ascii。如果 ascii 中不存在字母,则应将其转换为最接近的 ascii 表示形式。 我正在使用 Unidecode 工具 (https://pypi.python.org/pypi/Unidecode)。当我在 CL 上的 Python 解释器中使用它时,它工作正常(因此,通过调用 python 然后导入库,然后像这样打印解码后的单词:print unidecode(u'äèß')

不幸的是,当我尝试直接在命令行上使用此工具时(因此,通过执行python -c "from unidecode import *; print unidecode(u'äèß')" 之类的操作,它只会打印乱码(准确地说是A$?A"A,即使它应该已经打印(并且确实在解释器中)aess)。这很烦人,我不知道如何解决这个问题。我认为这可能是由于我的终端编码错误,没有正确设置为 utf-8 或其他东西。但是, locale 在我的终端中打印出以下输出:

LANG="de_DE.UTF-8"

LC_COLLATE="de_DE.UTF-8"

LC_CTYPE="de_DE.UTF-8"

LC_MESSAGES="de_DE.UTF-8"

LC_MONETARY="de_DE.UTF-8"

LC_NUMERIC="de_DE.UTF-8"

LC_TIME="de_DE.UTF-8"

LC_ALL="de_DE.UTF-8"

或者,可能是由于 Python 在命令行上的 StdIn 编码存在问题?它在 python 解释器中给了我正确的输出,但是在调用 python -c 时没有。

你们有什么想法吗?

【问题讨论】:

    标签: python encoding command-line utf-8


    【解决方案1】:

    当您在终端中键入“äèß”时,虽然您看到“äèß”,但终端看到的是字节。如果你的终端编码是utf-8,那么它会看到字节

    In [2]: 'äèß'
    Out[2]: '\xc3\xa4\xc3\xa8\xc3\x9f'
    

    所以当你输入时

    python -c "from unidecode import *; print unidecode(u'äèß')"
    

    在命令行,终端(假设为utf-8编码)看到

    python -c "from unidecode import *; print unidecode(u'\xc3\xa4\xc3\xa8\xc3\x9f')"
    

    这不是您打算发送到 Python 的 unicode。

    In [28]: print(u'\xc3\xa4\xc3\xa8\xc3\x9f')
    äèÃ
    

    有很多方法可以解决这个问题,也许是为了方便:

    1. 让终端把äèß改成\xc3\xa4\xc3\xa8\xc3\x9f然后 解码为utf-8:

      % python -c "from unidecode import *; print unidecode('äèß'.decode('utf_8'))"
      aess
      
    2. 声明一个编码,如 Nehal J. Wani 的解决方案所示:

      % python -c "#coding: utf8
      > from unidecode import *; print unidecode(u'äèß')" 
      aess
      

      然而,这需要将命令写成两行。

    3. 因为u'äèß 等同于u'\xe4\xe8\xdf' 你可以避免 通过传递u'\xe4\xe8\xdf' 来解决问题:

      % python -c "from unidecode import *; print unidecode(u'\xe4\xe8\xdf')"
      aess
      

      这样做的问题(显然)是你必须弄清楚 出十六进制代码点值。

    4. 或者,您可以按名称指定 unicode:

      % python -c "from unidecode import *; print unidecode(u'\N{LATIN SMALL LETTER A WITH DIAERESIS}\N{LATIN SMALL LETTER E WITH GRAVE}\N{LATIN SMALL LETTER SHARP S}')"
      aess
      

    【讨论】:

    • 因为我会将它集成到一个脚本中,为大量文件进行转换,所以 1 对我来说似乎是一个非常好的解决方案!
    【解决方案2】:

    如果您尝试将其写入文件:

    #!/bin/python
    from unidecode import *
    print unidecode(u'äèß')
    
    [Wani@Linux tmp]$ python tmp.py 
    File "tmp.py", line 1
    SyntaxError: Non-ASCII character '\xc3' in file tmp.py on line 1, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
    [Wani@Linux tmp]$ 
    

    要解决此问题,您可以:

    #!/bin/python
    #coding: utf8
    from unidecode import *; print unidecode(u'äèß')
    
    [Wani@Linux tmp]$ python tmp.py
    aeess
    [Wani@Linux tmp]$
    

    所以,你需要像这样从命令行调用:

    [Wani@Linux tmp]$ python -c "#coding: utf8
    from unidecode import *; print unidecode(u'äèß')"
    aeess
    [Wani@Linux tmp]$ python -c "$(echo -e "#coding: utf8\nfrom unidecode import *; print unidecode(u'äèß')")"
    aeess
    [Wani@Linux tmp]
    

    延伸阅读:Correct way to define Python source code encoding

    【讨论】:

    • 我不知道我能在两条线上做到这一点,酷!不过,对于我的应用程序,unutbu 的解决方案似乎有效。
    猜你喜欢
    • 1970-01-01
    • 2011-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-15
    • 1970-01-01
    • 2011-07-02
    相关资源
    最近更新 更多