首页 > 解决方案 > 如何在ini或cfg文档中保存=字符?

问题描述

我想在ini文档中保存磁铁链接。因此,我不可避免地必须在文件中保存“=”字符。但是,python认为“=”字符是“值的选项”,所以python空闲返回“configparser.DuplicateOptionError :从'history.ini' [第3行]读取时:'0'部分中的选项'磁铁'已经存在“当我使用

configparser.ConfigParser().read('history.ini')

如果您有任何想法来处理这个问题,请让我知道,提前谢谢。

标签: pythonpython-3.xinihibernate.cfg.xml

解决方案


I cannot reproduce the problem. I can save values containing = characters just fine:

test.ini

[Section]
Key=Val=ue

test.py

import configparser
cp = configparser.ConfigParser()
cp.read('ini.ini')
print(cp['Section']['Key'])  # Val=ue

I think the actual issue is just that you use the magnet key twice in the config (two lines which both start with magnet=).

If you want to have a list of multiple magnet links you can try using something like magnet00000=, magnet00001=, magnet00002=, and so on. Or switch to JSON.


推荐阅读