简体   繁体   中英

Reading array from config file in python

I have a problem. My program is using config file to set options, and one of those options is a tuple. Here's what i mean:

[common]
logfile=log.txt
db_host=localhost
db_user=root
db_pass=password
folder[1]=/home/scorpil
folder[2]=/media/sda5/
folder[3]=/media/sdb5/

etc... Can i parse this into tuple with ConfigParser module in Python? Is there some easy way to do this?

if you can change config format like this:

folder = /home/scorpil
         /media/sda5/
         /media/sdb5/

then in python:

config.get("common", "folder").split("\n")

Your config could be:

[common]
logfile=log.txt
db_host=localhost
db_user=root
db_pass=password
folder = ("/home/scorpil", "/media/sda5/", "/media/sdb5/")

Assuming that you have config in a file named foo.cfg, you can do the following:

import ConfigParser
cp = ConfigParser.ConfigParser()
cp.read("foo.cfg")
folder = eval(cp.get("common", "folder"), {}, {})

print folder
print type(folder)

which should produce:

('/home/scorpil', '/media/sda5/', '/media/sdb5/')
<type 'tuple'>

-- EDIT -- I've since changed my mind about this, and would take the position today that using eval in this context is a bad idea. Even with a restricted environment, if the configuration file is under user control it may be a very bad idea. Today I'd probably recommend doing something interesting with split to avoid malicious code execution.

您可以获取项目列表并使用列表理解在您的案例文件夹中创建名称以定义的前缀开头的所有项目的列表

folders = tuple([ item[1] for item in configparser.items() if item[0].startswith("folder")])

Create configuration:

folders = ['/home/scorpil', '/media/sda5/', '/media/sdb5/']
config.set('common', 'folders', json.dumps(folders))

Load configuration:

tuple(json.loads(config.get('common', 'folders')))

我不知道 ConfigParser,但您可以轻松地将其读入列表(也许使用.append() )然后执行myTuple = tuple(myList)

#!/usr/bin/env python
sample = """
[common]
logfile=log.txt
db_host=localhost
db_user=root
db_pass=password
folder[1]=/home/scorpil
folder[2]=/media/sda5/
folder[3]=/media/sdb5/
"""
from cStringIO import StringIO
import ConfigParser
import re
FOLDER_MATCH = re.compile(r"folder\[(\d+)\]$").match

def read_list(items,pmatch=FOLDER_MATCH):
    if not hasattr(pmatch,"__call__"):
        pmatch = re.compile(pmatch).match
    folder_list = []
    for k,v in items:
        m = pmatch(k)
        if m:
            folder_list.append((int(m.group(1)),v))
    return tuple( kv[1] for kv in sorted(folder_list) )


if __name__ == '__main__':
    cp = ConfigParser.SafeConfigParser()
    cp.readfp(StringIO(sample),"sample")

    print read_list(cp.items("common"))

You could stick to json completely

tst.json

{
  "common": {
    "logfile":"log.txt",
    "db_host":"localhost",
    "db_user":"root",
    "db_pass":"password",
    "folder": [
      "/home/scorpil",
      "/media/sda5/",
      "/media/sdb5/"
    ]
  }
}

then work with it

$ python3

>>> import json
>>> with open("tst.json", "r", encoding="utf8") as file_object:
...   job = json.load(file_object)
... 
>>> job
{'common': {'db_pass': 'password', 'logfile': 
'log.txt', 'db_user': 'root', 'folder': 
['/home/scorpil', '/media/sda5/', '/media/sdb5/'], 
'db_host': 'localhost'}}

>>> print (job["common"]["folder"][0])
/home/scorpil
>>> print (job["common"]["folder"][1])
/media/sda5/
print (job["common"]["folder"][2])
/media/sdb5/

>>> folder_tuple = tuple(job["common"]["folder"])
>>> folder_tuple
('/home/scorpil', '/media/sda5/', '/media/sdb5/')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM