【问题标题】:Dealing with multi-language directories (Python)处理多语言目录(Python)
【发布时间】:2010-06-20 18:11:17
【问题描述】:

我正在尝试打开一个文件,但我刚刚意识到 py 我的用户名有问题(它是俄语)。有关如何正确解码/编码以使空闲快乐的任何建议?

我正在使用 py 2.6.5

xmlfile = open(u"D:\\Users\\Эрик\\Downloads\\temp.xml", "r")

Traceback (most recent call last):
  File "<pyshell#23>", line 1, in <module>
    xmlfile = open(str(u"D:\\Users\\Эрик\\Downloads\\temp.xml"), "r")
UnicodeEncodeError: 'ascii' codec can't encode characters in position 9-12: ordinal not in range(128)

os.sys.getfilesystemencoding() 'mbcs'

xmlfile = open(u"D:\Users\Эрик\Downloads\temp.xml".encode("mbcs"), "r")

Traceback(最近一次调用最后一次): 文件“”,第 1 行,在 xmlfile = open(u"D:\Users\Эрик\Downloads\temp.xml".encode("mbcs"), "r") IOError: [Errno 22] 无效模式 ('r') 或文件名:'D:\Users\Y?ee\Downloads\temp.xml'

【问题讨论】:

    标签: python unicode decode


    【解决方案1】:

    第一个问题是解析器试图解释字符串中的反斜杠,除非您使用r"raw quote" 前缀。在 2.6.5 中,您不需要特别对待 Unicode 字符串,但您可能需要在源代码中声明文件编码,例如:

    # -*- coding: utf-8 -*-
    

    PEP 263 中所定义。以下是它以交互方式工作的示例:

    $ python
    Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56) [GCC 4.4.3] on linux2
    >>> f = r"D:\Users\Эрик\Downloads\temp.xml"
    >>> f
    'D:\\Users\\\xd0\xad\xd1\x80\xd0\xb8\xd0\xba\\Downloads\\temp.xml'
    >>> x = open(f, 'w')
    >>> x.close()
    >>> 
    $ ls D*
    D:\Users\Эрик\Downloads\temp.xml
    

    是的,这是在 Unix 系统上,所以 \ 没有意义,我的终端编码是 utf-8,但它可以工作。您可能只需要在解析器读取文件时向它提供编码提示。

    【讨论】:

      【解决方案2】:

      第一个问题:

      xmlfile = open(u"D:\\Users\\Эрик\\Downloads\\temp.xml", "r")
      ### The above line should be OK, provided that you have the correct coding line
      ### For example # coding: cp1251
      
      Traceback (most recent call last):
        File "<pyshell#23>", line 1, in <module>
          xmlfile = open(str(u"D:\\Users\\Эрик\\Downloads\\temp.xml"), "r")
      ### HOWEVER the above traceback line shows you actually using str()
      ### which is DIRECTLY causing the error because it is attempting
      ### to decode your filename using the default ASCII codec -- DON'T DO THAT.
      ### Please copy/paste; don't type from memory.
      UnicodeEncodeError: 'ascii' codec can't encode characters in position 9-12: ordinal not in range(128)
      

      第二个问题:

      os.sys.getfilesystemencoding() 产生'mbcs'

      xmlfile = open(u"D:\Users\Эрик\Downloads\temp.xml".encode("mbcs"), "r")
      ### (a) \t is interpreted as a TAB character, hence the file name is invalid.
      ### (b) encoding with mbcs seems not to be useful; it messes up your name ("Y?ee").
      
      Traceback (most recent call last):
      File "", line 1, in xmlfile = open(u"D:\Users\Эрик\Downloads\temp.xml".encode("mbcs"), "r")
      IOError: [Errno 22] invalid mode ('r') or filename: 'D:\Users\Y?ee\Downloads\temp.xml'
      

      有关在 Windows 中硬编码文件名的一般建议,按偏好降序排列:

      (1) 不要
      (2) 使用/ 例如"c:/temp.xml"
      (3) 使用带反斜杠的原始字符串r"c:\temp.xml"
      (4) 使用双反斜杠"c:\\temp.xml"

      【讨论】:

        猜你喜欢
        • 2016-02-23
        • 2019-06-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多