本文介绍了ConfigParser:写一个列表但是读取一个字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看起来ConfigParser会接受一个列表来写入

* .ini文件;但是当它重新读回来时,它将它视为一个字符串。


示例:


########## #####################

import ConfigParser

def whatzit(thingname,thing):

print thingname," value:",

print thingname," length:",len(thing)

print thingname,type (东西)


cfgfile =" cfgtest.ini"

config1 = ConfigParser.ConfigParser()

config1.add_section( 测试)


t1 =范围(1,11)

config1.set(" test"," testlist",t1)

outfile = open(cfgfile," w")

config1.write(outfile)

outfile.close()


config2 = ConfigParser.ConfigParser()

config2.read(cfgfile)

t2 = config2.get(" test"," testlist" ;)


whatzit(" t1",t1)

whatzit(" t2",t2)


###################### #########


输出为:


t1值:[1,2,3,4,5, 6,7,8,9,10]

t1长度:10

t1< type''list''>

t2值:[1,2,3,4,5,6,7,8,9,10]

t2长度:31

t2< type''str ''>


也就是说,t1是一个长度为10的列表,包括:

[1,2,3,4,5,6 ,7,8,9,10]

并写出来;但是t2以字符串形式回读

[1,2,3,4,5,6,7,8,9,10]

长度为31.


我花了一段时间才弄明白这一点,因为它们在

打印报表中看起来相同。


是否有一种pythonic方式从.INI文件中读取列表

ConfigParser?这是ConfigParser的预期行为吗?我想

不要指望这种转换;相反,如果不支持列表,则尝试写入列表时会出现例外情况。

It looks like ConfigParser will accept a list to be writing to the
*.ini file; but when reading it back in, it treats it as a string.

Example:

###############################
import ConfigParser
def whatzit(thingname, thing):
print thingname, "value:", thing
print thingname, "length:", len(thing)
print thingname, type(thing)

cfgfile = "cfgtest.ini"
config1 = ConfigParser.ConfigParser()
config1.add_section("test")

t1 = range(1,11)
config1.set("test", "testlist", t1)
outfile=open(cfgfile,"w")
config1.write(outfile)
outfile.close()

config2 = ConfigParser.ConfigParser()
config2.read(cfgfile)
t2 = config2.get("test","testlist")

whatzit("t1", t1)
whatzit("t2", t2)

###############################

Output is:

t1 value: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
t1 length: 10
t1 <type ''list''>
t2 value: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
t2 length: 31
t2 <type ''str''>

That is, t1 is a list of length 10, consisting of:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
and is written out; but t2 is read back in as a string
"[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]"
of length 31.

It took me a while to figure this out, since they looked identical in
print statements.

Is there a pythonic way to read in a list from a .INI file with
ConfigParser? Is this expected behavior for ConfigParser? I would
not expect this conversion; rather, an exception when trying to write
the list if the list is not supported.

推荐答案




我同意你的意见。


Sybren

-

世界的问题是愚蠢。并不是说应该对愚蠢的死刑进行处罚,但为什么我们不要仅仅拿掉

安全标签来解决问题呢? br />
Frank Zappa



I agree with you.

Sybren
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don''t we just take the
safety labels off of everything and let the problem solve itself?
Frank Zappa






如果您想要两种文本格式的配置文件和类型使用

" ZConfig"相反。


PS:


ZConfig没有内置的编写配置方式 - 但这不是一个

严重限制:如果配置被拆分为静态,则在运行之前更新

和state-。应用程序改变了一些文件的集合,更容易保持配置一致

无论如何(将dict转换为ZConfig格式并不困难) )。



If you want both text format config files and types that work then use
"ZConfig" instead.

PS:

ZConfig has no built-in way of writing a configuration - but this is not a
severe limitation: If the configuration is split into a "static-", which get
updated before a run and a "state-" set of files with things that are
changed by the application it is easier to keep the configuration consistent
anyway (it is not so hard to dump a dict into a ZConfig format).


这篇关于ConfigParser:写一个列表但是读取一个字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 03:25