使用StringIO for ConfigObj和Unicode

Mar*_*eux 5 python unicode configobj

我正在尝试使用StringIO来提供ConfigObj.我想在单元测试中执行此操作,以便我可以动态地模拟配置"文件",具体取决于我要在配​​置对象中测试的内容.

我在配置模块中有很多事情要处理(我正在阅读其他应用程序的几个conf文件,聚合和"格式化"信息).但是,在测试中,我面临着来自地狱的unicode错误.我想我已经把我的问题归结为最小的功能代码,我已经提取并过度简化了这个问题的目的.

我正在做以下事情:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import configobj
import io

def main():
    """Main stuff"""

    input_config = """
    [Header]
    author = PloucPlouc
    description = Test config

    [Study]
    name_of_study = Testing
    version = 9999
    """

    # Just not to trust my default encoding
    input_config = unicode(input_config, "utf-8")

    test_config_fileio = io.StringIO(input_config)    
    print configobj.ConfigObj(infile=test_config_fileio, encoding="UTF8")

if __name__ == "__main__":
    main()
Run Code Online (Sandbox Code Playgroud)

它产生以下回溯:

Traceback (most recent call last):
File "test_configobj.py", line 101, in <module>
    main()
File "test_configobj.py", line 98, in main
    print configobj.ConfigObj(infile=test_config_fileio, encoding='UTF8')
File "/work/irlin168_1/USER/Apps/python272/lib/python2.7/site-packages/configobj-4.7.2-py2.7.egg/configobj.py", line 1242, in __init__
    self._load(infile, configspec)
File "/work/irlin168_1/USER/Apps/python272/lib/python2.7/site-packages/configobj-4.7.2-py2.7.egg/configobj.py", line 1302, in _load
    infile = self._handle_bom(infile)
File "/work/irlin168_1/USER/Apps/python272/lib/python2.7/site-packages/configobj-4.7.2-py2.7.egg/configobj.py", line 1442, in _handle_bom
    if not line.startswith(BOM):
UnicodeDecodeError: 'ascii' codec can't decode byte 0xef in position 0: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)

我在linux上使用Python-2.7.2(32位).我对控制台和编辑器(Kile)的语言环境设置为fr_FR.utf8.

我以为我能做到这一点.

io.StringIO文档中,我得到了这个:

StringIO对象可以接受Unicode或8位字符串,但混合两者可能需要一些小心.

ConfigObj文档中,我可以这样做:

>>> config = ConfigObj('config.ini', encoding='UTF8')
>>> config['name']
    u'Michael Foord'
Run Code Online (Sandbox Code Playgroud)

:

infile:无

您不需要指定infile.如果省略它,将创建一个空的ConfigObj.infile可以是:

   [...]
   A StringIO instance or file object, or any object with a read method. The filename attribute of your ConfigObj will be None [5].
Run Code Online (Sandbox Code Playgroud)

'encoding':无

默认情况下,ConfigObj不会将您传递给Unicode [8]的文件/字符串进行解码.如果您希望配置文件为Unicode(密钥和成员),则需要提供一个编码来解码文件.编写时,此编码也将用于编码配置文件.

我的问题是为什么会产生这个?还有哪些(简单的)Unicode处理无法理解?...

通过查看这个答案,我改变了:

input_config = unicode(input_config, "utf8")
Run Code Online (Sandbox Code Playgroud)

to(导入编解码器模块breforehand):

input_config = unicode(input_config, "utf8").strip(codecs.BOM_UTF8.decode("utf8", "strict"))
Run Code Online (Sandbox Code Playgroud)

为了摆脱可能包含的字节顺序标记,但它没有帮助.

非常感谢

注意:如果我使用StringIO.StringIO而不是io.StringIO,我有相同的回溯.

Mar*_*nen 4

这行:

input_config = unicode(input_config, "utf8")
Run Code Online (Sandbox Code Playgroud)

正在将您的输入转换为 Unicode,但是这一行:

print configobj.ConfigObj(infile=test_config_fileio, encoding="UTF8")
Run Code Online (Sandbox Code Playgroud)

将输入声明为 UTF-8 编码的字节字符串。该错误表明在需要字节字符串时传递了 Unicode 字符串,因此注释掉上面的第一行应该可以解决该问题。configobj我暂时没有,所以无法测试。