Python配置解析器特殊ch

2024-06-25 06:37:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试使用configparser模块,但有一点问题。在

以下是代码示例:

import configparser

ini_file = """[s1]\nl1 = 01\n= = 02\n[s2]\n l2 = 123"""
print (ini_file)

config = configparser.ConfigParser()
config.read_string(ini_file)
config.sections()

我可以在=或:?等键中使用空格字符吗?在

(示例==02,其中first=为键,02为value)

(在ANSI-C中,我可以使用\“to print”--特殊字符)。可能在python中类似:)

谢谢你的回答!在


Tags: 模块代码importconfig示例readiniconfigparser
1条回答
网友
1楼 · 发布于 2024-06-25 06:37:15

不,是的。在

否:Python使用“\”来转义字符串中的特殊字符,而不是代码中的特殊字符。re模块还使用“\”转义re字符串中的特殊字符。Configparser不使用“\”或任何其他转义字符。在

是:“=”和“:”是键和值之间的默认分隔符,但仅为默认分隔符。可以在Configparser(delimiters=<something else>)调用中使用delimiters关键字参数更改(对于整个文件)。来自configparser doc

delimiters, default value: ('=', ':')

Delimiters are substrings that delimit keys from values within a section. The first occurrence of a delimiting substring on a line is considered a delimiter. This means values (but not keys) can contain the delimiters.

相关问题 更多 >