我尝试使用 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,其中第一个 = 是键,02 是值)
(在 ANSI-C 中我可以使用\"来打印 "- 特殊字符)。也许在 python 中它是相似的:)
感谢解答!
请您参考如下方法:
不,是。
否:Python 使用'\' 来转义字符串中的特殊字符,但不用于代码中。 re 模块还使用 '\' 来转义 re 字符串中的特殊字符。 Configparser 不使用“\”或任何其他转义字符。
是的:“=”和“:”是键和值之间的默认分隔符,但只是默认值。可以使用 delimiters 更改它们(对于整个文件) Configparser(delimiters=<something else>) 中的关键字参数称呼。来自 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.
