簡體   English   中英

Python configparser讀取字典

[英]Python configparser read a dictionary

我想知道是否可以在配置文件中建立字典並使用python配置解析器讀取它?

謝謝。

使用eval並簡單地執行配置文件。

with open('the_config','r') as config_file:
    config= eval( config_file.read() )

您將看到評論告訴您這是邪惡的,並存在安全漏洞和許多其他問題。 但是,它和您的Python源代碼一樣安全。

configparser不支持該功能,但是也許您可能對看看json模塊感興趣。

改編自官方文檔的示例:

>>> import json
>>> s = json.dumps({'4': 5, '6': 7}, sort_keys=True, indent=4)
>>> print(s)
{
    "4": 5, 
    "6": 7
}
>>> json.loads(s)
{'4': 5, '6': 7}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM