【问题标题】:Python - configuration filePython - 配置文件
【发布时间】:2019-10-17 11:53:55
【问题描述】:

我有代码:

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】:

    我猜你需要这样取值才能得到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

    【讨论】:

      猜你喜欢
      • 2019-01-14
      • 2014-03-27
      • 1970-01-01
      • 2011-10-02
      • 1970-01-01
      • 1970-01-01
      • 2011-03-04
      • 2016-03-16
      • 1970-01-01
      相关资源
      最近更新 更多