您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

python – 使用StringIO for ConfigObj和Unicode

5b51 2022/1/14 8:20:54 python 字数 8140 阅读 440 来源 www.jb51.cc/python

我正在尝试使用StringIO来提供ConfigObj.我想在单元测试中执行此操作,以便我可以动态地模拟配置“文件”,具体取决于我要在配??置对象中测试的内容.我在配置模块中有很多事情要处理(我正在阅读其他应用程序的几个conf文件,聚合和“格式化”信息).但是,在测试中,我面临着来自地狱的unicode错误.我想我已经把我的问题归结为最小的功能代码,我已经

概述

我正在尝试使用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()

它产生以下回溯:

Traceback (most recent call last):
File "test_configobj.py",line 101,in 
  
   figobj.py",line 98,in main
    print con
   figobj.Con
   figObj(infile=test_con
   fig_fileio,encoding='UTF8')
File "/work/irlin168_1/USER/Apps/python272/lib/python2.7/site-packages/con
   figobj-4.7.2-py2.7.egg/con
   figobj.py",line 1242,in 
   __init__
    self._load(infile,con
   figspec)
File "/work/irlin168_1/USER/Apps/python272/lib/python2.7/site-packages/con
   figobj-4.7.2-py2.7.egg/con
   figobj.py",line 1302,in _load
    infile = self._handle_bom(infile)
File "/work/irlin168_1/USER/Apps/python272/lib/python2.7/site-packages/con
   figobj-4.7.2-py2.7.egg/con
   figobj.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开始,我可以这样做:

06002

this

infile: None

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

06003

‘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")

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

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 for ConfigObj和Unicode全部内容,希望文章能够帮你解决python – 使用StringIO for ConfigObj和Unicode所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶