【问题标题】:How do I fix - 'File Containms no Section Headers error?如何修复 - '文件不包含节标题错误?
【发布时间】:2020-01-01 16:26:17
【问题描述】:

尝试使用此代码从 .ini 文件中获取一些数据,当我尝试打开 .ini 时,它给出了我做错了什么?

config = configparser.ConfigParser()
config.sections()
config.read('FILE.ini') #throws file contains no section headers
print(config['DEFAULT']['toAddr'])
x = config.get('DEFAULT', 'to')

.ini 文件

['DEFAULT']
to = "blah"

我在 Win 10 上使用 Python 3.7 64

【问题讨论】:

  • 预期行为是什么,当前的问题是什么? when I try to open the .ini it gives What Am I doing wrong? - 你漏掉了一部分句子吗?

标签: python ini


【解决方案1】:

ini 文件中的标题不应包含引号。

使用

[DEFAULT]
to = "blah"

而不是

['DEFAULT']
to = "blah"

【讨论】:

  • 不,同样的错误,不起作用。这里遗漏了一些东西,比它需要的复杂得多。
  • 尝试将 .ini 更改为 .cfg?没有喜悦,根本行不通。同样的错误。除了 configparser,还有其他存储用户设置的方法吗?它似乎在 3.7 64bit 中不起作用?
  • 感谢您的帮助,我得到了它的工作,到目前为止,不知道为什么这么想。
猜你喜欢
  • 1970-01-01
  • 2012-10-28
  • 2019-06-07
  • 1970-01-01
  • 2017-02-11
  • 2022-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多