[英]ConfigParser options with % in value
我正在嘗試使用 ConfigParser 解析 .conf 文件,其中一些值的值中有一個 '%'。
下面的配置示例:
[global]
workgroup = WORKGROUP
server string = %h server (Samba, Ubuntu)
當我使用 RawConfigParser 解析它時,它將server string
設置為
%h server (Samba, Ubuntu)\n\n\n\ndns proxy = no\n\n\n\n\n\n\n\n...REST_OF_CONFIG_SECTION
其中一堆\\n
是配置選項之間的空行。 當我使用普通的ConfigParser
,我收到一個InterpolationSyntaxError
錯誤
'%' 后面必須跟 '%' 或 '(', found: '%h server (Samba, Ubuntu)\\n\\n\\n\\ndns proxy = no\\n\\n\\n\\n\\n\\n\\ n\\n
在將字符串傳遞給 ConfigParser(用 %% 替換 %)之前編輯字符串會對其進行解析,但與 RawConfigParser 執行相同的問題,它將其余選項解析為一行。
我應該如何正確解析其中包含 % 的值?
請參閱文檔。 你必須使用 %% 來逃避 %
https://docs.python.org/3/library/configparser.html
[Escape]
gain: 80%% # use a %% to escape the % sign (% is the only character that needs to be escaped)
在你的情況下:
[global]
workgroup = WORKGROUP
server string = %%h server (Samba, Ubuntu)
在普通的 ConfigParser 中禁用插值:
configparser.ConfigParser(interpolation=None)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.