如何使用configparser插入多行值

问题描述 投票:3回答:1

我必须修改一个配置文件,该文件的结构与.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吗?

python configparser
1个回答
0
投票

解决方法:

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.