可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
The typical ConfigParser generated file looks like:
[Section]
bar=foo
[Section 2]
bar2= baz
Now, is there a way to index lists like, for instance:
[Section 3]
barList={
item1,
item2
}
Related question: Python’s ConfigParser unique keys per section
回答1:
There is nothing stopping you from packing the list into a delimited string and then unpacking it once you get the string from the config. If you did it this way your config section would look like:
[Section 3]
barList=item1,item2
It's not pretty but it's functional for most simple lists.
回答2:
Also a bit late, but maybe helpful for some.
I am using a combination of ConfigParser and JSON:
[Foo]
fibs: [1,1,2,3,5,8,13]
just read it with:
>>> json.loads(config.get("Foo","fibs"))
[1, 1, 2, 3, 5, 8, 13]
You can even break lines if your list is long (thanks @peter-smit):
[Bar]
files_to_check = [
"/path/to/file1",
"/path/to/file2",
"/path/to/another file with space in the name"
]
Of course i could just use JSON, but i find config files much more readable, and the [DEFAULT] Section very handy.
回答3:
Coming late to this party, but I recently implemented this with a dedicated section in a config file for a list:
[paths]
path1 = /some/path/
path2 = /another/path/
...
and using config.items( "paths" )
to get an iterable list of path items, like so:
path_items = config.items( "paths" )
for key, path in path_items:
#do something with path
Hope this helps other folk Googling this question ;)
回答4:
One thing a lot of people don't know is that multi-line configuration-values are allowed. For example:
;test.ini
[hello]
barlist =
item1
item2
The value of config.get('hello','barlist')
will now be:
"\nitem1\nitem2"
Which you easily can split with the splitlines method (don't forget to filter empty items).
If we look to a big framework like Pyramid they are using this technique:
def aslist_cronly(value):
if isinstance(value, string_types):
value = filter(None, [x.strip() for x in value.splitlines()])
return list(value)
def aslist(value, flatten=True):
""" Return a list of strings, separating the input based on newlines
and, if flatten=True (the default), also split on spaces within
each line."""
values = aslist_cronly(value)
if not flatten:
return values
result = []
for value in values:
subvalues = value.split()
result.extend(subvalues)
return result
Source
Myself, I would maybe extend the ConfigParser if this is a common thing for you:
class MyConfigParser(ConfigParser):
def getlist(self,section,option):
value = self.get(section,option)
return list(filter(None, (x.strip() for x in value.splitlines())))
def getlistint(self,section,option):
return [int(x) for x in self.getlist(section,option)]
Note that there are a few things to look out for when using this technique
- New lines that are items should start with whitespace (e.g. a space or a tab)
- All following lines that start with whitespace are considered to be part of the previous item. Also if it has an = sign or if it starts with a ; following the whitespace.
回答5:
If you want to literally pass in a list then you can use:
ast.literal_eval()
For example configuration:
[section]
option=["item1","item2","item3"]
The code is:
import ConfigParser
import ast
my_list = ast.literal_eval(config.get("section", "option"))
print(type(my_list))
print(my_list)
output:
<type'list'>
["item1","item2","item3"]
回答6:
I landed here seeking to consume this...
[global]
spys = richard.sorge@cccp.gov, mata.hari@deutschland.gov
The answer is to split it on the comma and strip the spaces:
SPYS = [e.strip() for e in parser.get('global', 'spys').split(',')]
To get a list result:
['richard.sorge@cccp.gov', 'mata.hari@deutschland.gov']
It may not answer the OP's question exactly but might be the simple answer some people are looking for.
回答7:
This is what I use for lists:
config file content:
[sect]
alist = a
b
c
code :
l = config.get('sect', 'alist').split('\n')
it work for strings
in case of numbers
config content:
nlist = 1
2
3
code:
nl = config.get('sect', 'alist').split('\n')
l = [int(nl) for x in nl]
thanks.
回答8:
No mention of the converters
kwarg for ConfigParser()
in any of these answers was rather disappointing.
According to the documentation you can pass a dictionary to ConfigParser
that will add a get
method for both the parser and section proxies. So for a list:
example.ini
[Germ]
germs: a,list,of,names, and,1,2, 3,numbers
Parser example:
cp = ConfigParser(converters={'list': lambda x: [i.strip() for i in x.split(',')]})
cp.read('example.ini')
cp.getlist('Germ', 'germs')
['a', 'list', 'of', 'names', 'and', '1', '2', '3', 'numbers']
cp['Germ'].getlist('germs')
['a', 'list', 'of', 'names', 'and', '1', '2', '3', 'numbers']
This is my personal favorite as no subclassing is necessary and I don't have to rely on an end user to perfectly write JSON or a list that can be interpreted by ast.literal_eval
.
回答9:
Only primitive types are supported for serialization by config parser. I would use JSON or YAML for that kind of requirement.
回答10:
I faced the same problem in the past. If you need more complex lists, consider creating your own parser by inheriting from ConfigParser. Then you would overwrite the get method with that:
def get(self, section, option):
""" Get a parameter
if the returning value is a list, convert string value to a python list"""
value = SafeConfigParser.get(self, section, option)
if (value[0] == "[") and (value[-1] == "]"):
return eval(value)
else:
return value
With this solution you will also be able to define dictionaries in your config file.
But be careful! This is not as safe: this means anyone could run code through your config file. If security is not an issue in your project, I would consider using directly python classes as config files. The following is much more powerful and expendable than a ConfigParser file:
class Section
bar = foo
class Section2
bar2 = baz
class Section3
barList=[ item1, item2 ]
回答11:
import ConfigParser
import os
class Parser(object):
"""attributes may need additional manipulation"""
def __init__(self, section):
"""section to retun all options on, formatted as an object
transforms all comma-delimited options to lists
comma-delimited lists with colons are transformed to dicts
dicts will have values expressed as lists, no matter the length
"""
c = ConfigParser.RawConfigParser()
c.read(os.path.join(os.path.dirname(__file__), 'config.cfg'))
self.section_name = section
self.__dict__.update({k:v for k, v in c.items(section)})
#transform all ',' into lists, all ':' into dicts
for key, value in self.__dict__.items():
if value.find(':') > 0:
#dict
vals = value.split(',')
dicts = [{k:v} for k, v in [d.split(':') for d in vals]]
merged = {}
for d in dicts:
for k, v in d.items():
merged.setdefault(k, []).append(v)
self.__dict__[key] = merged
elif value.find(',') > 0:
#list
self.__dict__[key] = value.split(',')
So now my config.cfg
file, which could look like this:
[server]
credentials=username:admin,password:$3<r3t
loggingdirs=/tmp/logs,~/logs,/var/lib/www/logs
timeoutwait=15
Can be parsed into fine-grained-enough objects for my small project.
>>> import config
>>> my_server = config.Parser('server')
>>> my_server.credentials
{'username': ['admin'], 'password', ['$3<r3t']}
>>> my_server.loggingdirs:
['/tmp/logs', '~/logs', '/var/lib/www/logs']
>>> my_server.timeoutwait
'15'
This is for very quick parsing of simple configs, you lose all ability to fetch ints, bools, and other types of output without either transforming the object returned from Parser
, or re-doing the parsing job accomplished by the Parser class elsewhere.
回答12:
json.loads & ast.literal_eval seems to be working but simple list within config is treating each character as byte so returning even square bracket....
meaning if config has fieldvalue = [1,2,3,4,5]
then config.read(*.cfg)
config['fieldValue'][0] returning "[" in place of 1