python - 将 StringIO 用于 ConfigObj 和 Unicode

标签 python unicode configobj

我正在尝试使用 StringIO 来提供 ConfigObj。 我想在我的单元测试中执行此操作,以便我可以根据我想在配置对象中测试的内容动态模拟配置"file"。

我在配置模块中处理了一大堆事情(我正在读取几个 conf 文件,为其余应用程序聚合和“格式化”信息)。但是,在测试中,我遇到了来自 hell 的 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()

它产生以下回溯:

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)

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

我以为我可以做到这一点。

来自io.StringIO documentation ,我得到了这个:

The StringIO object can accept either Unicode or 8-bit strings, but mixing the two may take some care.

来自ConfigObj documentation ,我可以这样做:

>>> config = ConfigObj('config.ini', encoding='UTF8')
>>> config['name']
    u'Michael Foord'

this :

infile: None

You don't need to specify an infile. If you omit it, an empty ConfigObj will be created. infile can be :

   [...]
   A StringIO instance or file object, or any object with a read method. The filename attribute of your ConfigObj will be None [5].

'encoding': None

By default ConfigObj does not decode the file/strings you pass it into Unicode [8]. If you want your config file as Unicode (keys and members) you need to provide an encoding to decode the file with. This encoding will also be used to encode the config file when writing.

我的问题是它为什么会产生这个?我还没有从(简单的)Unicode 处理中理解什么?...

通过查看此 answer ,我改变了:

input_config = unicode(input_config, "utf8")

到(预先导入编解码器模块):

input_config = unicode(input_config, "utf8").strip(codecs.BOM_UTF8.decode("utf8", "strict"))

为了去掉可能包含的字节顺序标记,但没有帮助。

非常感谢

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

最佳答案

这一行:

input_config = unicode(input_config, "utf8")

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

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

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

关于python - 将 StringIO 用于 ConfigObj 和 Unicode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11830480/

相关文章:

python - Flask 允许多个服务器实例监听同一个端口

python - 过滤 Pandas Dataframe 或 Series 中的值

Python Click 在执行前确定链式命令的顺序

python - ConfigObj 长列表和默认值

Python 3.3安装 "configobj"

python - 如何在多类分类的 light gbm python 中编写自定义 F1 分数指标

.net - 定期 - 无法将索引 Y 处的 Unicode 字符 X 转换为指定的代码页

c# - 有什么理由比 UTF-8 更喜欢 UTF-16?

windows - 如何从角色中找出语言?

python - 如何在Python中使用configobj保留引号字符串