简体   繁体   English

Configparser 和带有 % 的字符串

[英]Configparser and string with %

Stupid question with (for sure) simple answer...愚蠢的问题(当然)简单的答案......

I am using configparser to read some strings from a file.我正在使用 configparser 从文件中读取一些字符串。 When the string has the '%' symbol ($%& for example) it complains:当字符串具有 '%' 符号(例如 $%&)时,它会抱怨:

ConfigParser.InterpolationSyntaxError: '%' must be followed by '%' or '(', found: "%&'" ConfigParser.InterpolationSyntaxError: '%' 后面必须跟 '%' 或 '(', found: "%&'"

Anybody familiar with this?有人熟悉这个吗?

Thanks!谢谢!

如果您不想替换环境变量,请使用 RawConfigParser,而不是 ConfigParser。

Write two % :两个%

V = ('%%', 'MHz', 'GHz')

result:结果:

('%', 'MHz', 'GHz')

In newer Python versions, use在较新的 Python 版本中,使用

configParser = configparser.ConfigParser(interpolation=None)

This disables interpolation.这将禁用插值。

Note that RawConfigParser is a legacy variant.请注意, RawConfigParser是一个遗留变体。 From the python docs :python 文档

Consider using ConfigParser instead which checks types of the values to be stored internally.考虑使用 ConfigParser 来检查要在内部存储的值的类型。 If you don't want interpolation, you can use ConfigParser(interpolation=None).如果你不想插值,你可以使用 ConfigParser(interpolation=None)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM