Python-配置文件

问题描述 投票:-1回答:1

我有代码:

import matplotlib.pyplot as plt
from configparser import ConfigParser  
cfg = ConfigParser()
cfg.read('file.cfg')    
plt.plot([1, 10],[2, 2], color_4, ls = "dashed")   
plt.xlim(1,10)
plt.ylim(1,4)
plt.savefig('image.pdf')

而且我想通过配置文件控制它:

[a]
color_4 = c = 'silver'

请问怎么了?它给出了一个错误:

NameError: name 'color_4' is not defined
python-3.x config configparser
1个回答
1
投票

我想您需要以这种方式获取值才能获取color_4的值:

cfg['a']['color_4']

from configparser import ConfigParser  
cfg = ConfigParser()
cfg.read('file.cfg')    
plt.plot([1, 10],[2, 2], cfg['a']['color_4'], ls = "dashed")   
plt.xlim(1,10)
plt.ylim(1,4)
plt.savefig('image.pdf')

参考:ConfigParser

© www.soinside.com 2019 - 2024. All rights reserved.