ConfigParser:写一个列表但是读取一个字符串? [英] ConfigParser: writes a list but reads a string?

查看:99
本文介绍了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.

推荐答案

Terry Carroll用以下方式启发我们:
Terry Carroll enlightened us with:
看起来ConfigParser会接受一个列表来写入
* .ini文件;但是当读回来时,它会将其视为一个字符串。


手册中没有明确说明,但我确实发现了这个:


"""默认值必须适合%()s字符串

插值。"""


因此,如果默认值通过%s,也许所有值都可以。

是否有使用
ConfigParser从.INI文件中读取列表的pythonic方法?


我会挑选()列表,并存储它。然后你可以打开()

它并重新获得你的清单。

我不希望这种转换;相反,如果不支持列表,则尝试编写列表时会出现异常。
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.
It doesn''t say so explicitly in the manual, but I did find this:

"""The values in defaults must be appropriate for the "%()s" string
interpolation."""

So if the defaults go through %s, perhaps all values do.
Is there a pythonic way to read in a list from a .INI file with
ConfigParser?
I''d pickle() the list, and store that instead. Then you can unpicle()
it and regain your list.
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


ConfigObj将读取和写入列表值。


你会得到各种其他的优点也是(嵌套的子部分为

任何深度),结果代码会更简单。


来自configobj import ConfigObj

cfgfile =" cfgtest.ini"


cfg = ConfigObj(cfgfile)

t1 =范围(1,11)

#no *需要*创建一个小节

cfg [''test''] = {}

cfg [''test''] [''testlist ''] = t1

cfg.write()


将其读回:

来自configobj的
import ConfigObj

cfgfile =" cfgtest.ini"


cfg = ConfigObj(cfgfile)

print cfg [''test ''] [ '' てstlist'']

http:// www.voidspace.org.uk/python/configobj.html


一切顺利,


Fuzzyman
http://www.voidspace.org.uk/python/index。 shtml

ConfigObj will read and write list values.

You get all sorts of other advantages as well (nested subsections to
any depth), and the resulting code will be much simpler.

from configobj import ConfigObj
cfgfile = "cfgtest.ini"

cfg = ConfigObj(cfgfile)
t1 = range(1,11)
# no *need* to create a subsection
cfg[''test''] = {}
cfg[''test''][''testlist''] = t1
cfg.write()

To read it back in :

from configobj import ConfigObj
cfgfile = "cfgtest.ini"

cfg = ConfigObj(cfgfile)
print cfg[''test''][''testlist'']

http://www.voidspace.org.uk/python/configobj.html

All the best,

Fuzzyman
http://www.voidspace.org.uk/python/index.shtml




" Terry Carroll" < CA ***** @ nospam-tjc.com>在消息中写道

news:de ******************************** @ 4ax.com ...

"Terry Carroll" <ca*****@nospam-tjc.com> wrote in message
news:de********************************@4ax.com...
看起来ConfigParser会接受一个列表来写入
* .ini文件;但是当读回来时,它会将其视为一个字符串。


ConfigParser很讨厌因为它不支持类型转换

但仍然允许在配置中编写不兼容的类型 - 即

转换通常是单向的。它可能会以相同的方式在Floats中以b $
cro。 字符串插值是指字符串插值。也是以神秘的方式工作。


如果你需要ConfigParser工作,那么让每个配置项都是字符串,并且

必要时手动转换。

是否有一种pythonic方式可以使用
ConfigParser从.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.
ConfigParser is nasty because it does not really support type conversions
but still permits writing incompatible types to the configuration - i.e. the
conversions are generally one-way. It probably will croak on Floats in the
same way. The "string interpolation" works in mysterious ways too.

If you need ConfigParser to work then let every config item be strings and
convert manually as necessary.
Is there a pythonic way to read in a list from a .INI file with
ConfigParser? Is this expected behavior for ConfigParser?
Not in the way we normally use "expected" - but with ConfigParser it is to
be expected ;-)
I would
not expect this conversion; rather, an exception when trying to write
the list if the list is not supported.




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

" 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:写一个列表但是读取一个字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆