gpt4 book ai didi

python - 如何使用 configparser 插入多行值

转载 作者:行者123 更新时间:2023-12-05 07:52:16 24 4
gpt4 key购买 nike

我必须修改一个与 .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 实现吗?

最佳答案

解决方法:

您可以这样做的一种方法是将每一行添加到一个新选项中。喜欢:

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

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

关于python - 如何使用 configparser 插入多行值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33930852/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com