【问题标题】:Parsing .ini file with configparser - getting errors - wrong format of file使用 configparser 解析 .ini 文件 - 出现错误 - 文件格式错误
【发布时间】:2015-11-05 12:30:01
【问题描述】:

我想在 Python 2.6 中使用 ConfigParser 解析 *.ini 文件。 *.ini 文件结构如下所示:

>> Created by 1111
>> Date of creation: 2015.07.07
>> PlatForm: MS

[aaaa]
Author="bababa"
Version="22222"
[--]

[DIRECTORY]
v="c:\d\a\"
b="c:\d\b\"
n="c:\d\c\"
m="c:\d\e\"
z="c:\d\f\"
[--]

由于文件ConfigParser.MissingSectionHeaderError: File contains no section headers.的开头,configparser返回错误

我可以在不修改 *.ini 文件的情况下以某种方式解决此问题吗?

【问题讨论】:

  • 您可以将打开的文件句柄传递给readfp,因此您可以手动跳过前三行然后传入。
  • >> 序列应该是什么意思?
  • 这是无效的语法。使用; 表示评论。
  • 如何在不将所有行保存为字符串并先跳过三行的情况下修剪文件?
  • 按照@jonrsharpe 的建议去做。

标签: python config ini


【解决方案1】:
import ConfigParser
file = open('config.ini')
file.readline()
file.readline()
file.readline()
config = ConfigParser.ConfigParser()
config.readfp(file)

Python 2.6

【讨论】:

    猜你喜欢
    • 2016-07-27
    • 1970-01-01
    • 1970-01-01
    • 2018-03-29
    • 1970-01-01
    • 2017-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多