简体   繁体   English

使用configparser解析.ini文件-出现错误-文件格式错误

[英]Parsing .ini file with configparser - getting errors - wrong format of file

I would like to parse an *.ini file with ConfigParser with Python 2.6. 我想使用python 2.6使用ConfigParser解析* .ini文件。 The *.ini file structure looks like this: * .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\"
[--]

And configparser returns error because of the beginning of the file ConfigParser.MissingSectionHeaderError: File contains no section headers. 并且由于文件ConfigParser.MissingSectionHeaderError: File contains no section headers.的开头,configparser返回错误ConfigParser.MissingSectionHeaderError: File contains no section headers.

Can I somehow fix this without modifying *.ini file? 我能以某种方式解决此问题而不修改* .ini文件吗?

import ConfigParser
file = open('config.ini')
file.readline()
file.readline()
file.readline()
config = ConfigParser.ConfigParser()
config.readfp(file)

for Python 2.6 对于Python 2.6

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM