如何使用configparser插入多行值

时间:2015-11-26 04:54:00

标签: python configparser

我必须修改一个与.ini文件结构相似的配置文件。 但问题是配置文件有多行单值。如何修改?

例如,这里是我想要修改的配置。

[INCLUDES]

# Read common prefixes. If any customizations available -- read them from
# common.local
before = common.conf

[Definition]

_daemon = sshd


failregex = ^%(__prefix_line)s(?:error: PAM: )?[aA]uthentication (?:failure|error) for .* from <HOST>( via \S+)?\s*$
        ^%(__prefix_line)s(?:error: PAM: )?User not known to the underlying authentication module for .* from <HOST>\s*$
        ^%(__prefix_line)sFailed \S+ for .*? from <HOST>(?: port \d*)?(?: ssh\d*)?(: (ruser .*|(\S+ ID \S+ \(serial \d+\) CA )?\S+ %(__md5hex)s(, client user ".*", client host ".*")?))?\s*$
        ^%(__prefix_line)sROOT LOGIN REFUSED.* FROM <HOST>\s*$
        ^%(__prefix_line)s[iI](?:llegal|nvalid) user .* from <HOST>\s*$
        ^%(__prefix_line)sUser .+ from <HOST> not allowed because not listed in AllowUsers\s*$
        ^%(__prefix_line)sUser .+ from <HOST> not allowed because listed in DenyUsers\s*$
        ^%(__prefix_line)sUser .+ from <HOST> not allowed because not in any group\s*$
        ^%(__prefix_line)srefused connect from \S+ \(<HOST>\)\s*$
        ^%(__prefix_line)sReceived disconnect from <HOST>: 3: \S+: Auth fail$
        ^%(__prefix_line)sUser .+ from <HOST> not allowed because a group is listed in DenyGroups\s*$
        ^%(__prefix_line)sUser .+ from <HOST> not allowed because none of user's groups are listed in AllowGroups\s*$

ignoreregex =

如果我在编写字符串时使用\ n,则显示为

before = test1 \n test2 \n test3 \n test4

这可能是什么解决方案?甚至可以通过ConfigParser吗?

1 个答案:

答案 0 :(得分:0)

解决方法:

您可以通过将每行添加到新选项中来实现。喜欢:

[SECTION]
failregex = ...
replace 1 = ...
replace 2 = ...

之后,您只需打开ini文件作为文本文件,找到每个替换选项,然后用空格替换“替换1 =“。根据这篇文章,缩进足以让configparser将其作为多行值来处理: New lines with ConfigParser?