【问题标题】:Assigning more than one value to the same key in configuration file为配置文件中的同一个键分配多个值
【发布时间】:2013-02-18 10:38:12
【问题描述】:

我正在尝试创建一个配置文件,需要使用 ConfigParser 模块用 python 解析它

例如,我在文件 config.sr 中有以下设置

[basic]
basic=bz2,calendar,Core,ctype,curl,date,dom,ereg,exif,fileinfo,filter,ftp,gd,gettext,gmp,mhash,mysql,mysqli,openssl,pcntl,pcre,PDO,pdo_mysql,pdo_sqlite,Phar,readline

[advance]
advance=Reflection,session,shmop,SimpleXML,sockets,SPL,sqlite3,standard,tokenizer,wddx,xdebug,xml,xmlreader,xmlwriter,xsl,zip,zlib,Xdebug

所以你可以看到有更多的值(29) 用逗号分隔并分配给单个键basic,当我使用ConfigParser 模块解析它并获取结果时工作正常,但是分配的值声明时间过长。

当我在多行中分配它们时,它会显示一些错误,更多以逗号分隔的值的数量将进一步增加到 50,因此会导致一些可读性问题不断向前移动光标。

最后我想知道的是我们如何在配置的多行中声明与单个键相关的值?

示例格式

[basic]
basic=bz2,calendar,Core,ctype,curl,date,dom,ereg,exif,fileinfo,filter,ftp,gd,gettext,gmp,

hash,iconv,json,libxml,mbstring,mcrypt,mhash,mysql,mysqli,openssl,pcntl,pcre,PDO,pdo_mysql,

pdo_sqlite,Phar,readline

.............

【问题讨论】:

  • 问得好,但是'分配的值太长而无法声明'是什么意思?
  • 我的意思是,在上面的代码中,对于我分配的“基本”键,例如 bz2,calendar,Core,ctype .........,所以一段时间后我需要添加一些额外的名称相同的键“基本”增加了行长,所以我想声明每行只有 10 个值(bz2,日历,Core,ctype,...fileinfo),其他的也以相同的方式,我编辑上面的示例格式请找一样的

标签: python configparser configuration-files


【解决方案1】:

缩进它们:

[basic]
basic:
    bz2
    calendar
    Core
    ctype
    curl
    date
    dom
    ereg
    exif
    fileinfo
    filter
    ftp
    gd
    gettext
    gmp
    mhash
    mysql
    mysqli
    openssl
    pcntl
    pcre
    PDO
    pdo_mysql
    pdo_sqlite
    Phar
    readline

ConfigParser 然后会有一个列表,你可以用它做你喜欢的事情。

config = ConfigParser.ConfigParser()
config.read('config.sr')
basic_list = config.get('basic', 'basic').split('\n')

编辑:

运行代码如下: 我的config.sr

[basic]
basic:
    bz2
    calendar
    Core
    ctype
    curl
    date
    dom
    ereg
    exif
    fileinfo
    filter
    ftp
    gd
    gettext
    gmp
    mhash
    mysql
    mysqli
    openssl
    pcntl
    pcre
    PDO
    pdo_mysql
    pdo_sqlite
    Phar
    readline

[advanced]
advanced:
    a
    b
    c
    d
    e

还有我的python文件:q_14934291.py:

import ConfigParser

config = ConfigParser.ConfigParser()
config.read('config.sr')

basic_list = config.get('basic', 'basic').split('\n')
print('Basic list:')
print(basic_list)

advanced_list = config.get('advanced', 'advanced').split('\n')
print('\n\nAdvanced list:')
print(advanced_list)

运行输出:

Basic list:
['', 'bz2', 'calendar', 'Core', 'ctype', 'curl', 'date', 'dom', 'ereg', 'exif', 'fileinfo', 'filter', 'ftp', 'gd', 'gettext', 'gmp', 'mhash', 'mysql', 'mysqli', 'openssl', 'pcntl', 'pcre', 'PDO', 'pdo_mysql', 'pdo_sqlite', 'Phar', 'readline']


Advanced list:
['', 'a', 'b', 'c', 'd', 'e']

确保使用正确的缩进(4 个空格)。如果使用错误的缩进,会导致错误。

另外,您需要在条目上.split('\n')。抱歉,我忘记了那部分。

如果您不想在开头输入空条目,也可以strip 输入:

basic_list = config.get('basic', 'basic').strip().split('\n')

【讨论】:

  • 哦,给我一分钟,我会尝试让你知道
  • 不,它不工作,实际上什么也没发生,我在这里得到一个空白结果
  • ConfigParser.ParsingError: 文件包含解析错误:config.sr [line 3]: 'bz2\n' [line 4]: 'calendar\n' .....
  • 我已经修改了我的答案,我错过了将其转换为列表所需的 split 部分。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-12
相关资源
最近更新 更多