ConfigParser.MissingSectionHeaderError: File contains no section headers.

简介: 今天使用ConfigParser解析一个ini文件,报出如下错误: config.read(logFile) File "C:\Python26\lib\ConfigParser.py", line 286, in read self.

今天使用ConfigParser解析一个ini文件,报出如下错误:

复制代码
    config.read(logFile)
  File "C:\Python26\lib\ConfigParser.py", line 286, in read
    self._read(fp, filename)
  File "C:\Python26\lib\ConfigParser.py", line 482, in _read
    raise MissingSectionHeaderError(fpname, lineno, line)
MissingSectionHeaderError: File contains no section headers.
file: C:\test\test.ini, line: 1
'\xff\xfe\r\x00\n'
复制代码

经过分析是由于文件编码方式导致,调整代码如下,问题解决:

def test():
    logFile = r'C:\test\test.ini'
    config = ConfigParser.ConfigParser()  
    config.readfp(codecs.open(logFile, "r", "utf_16"))
    print config.sections()

此文件为unicode编码,所以这里填入"utf_16",如果为其他编码格式,则修改该参数。

python编码查询地址:http://docs.python.org/2/library/codecs.html?highlight=streamcodec#standard-encodings

目录
相关文章
|
3月前
|
Docker 容器
ADD failed: file not found in build context or excluded by .dockerignore: stat irap_rule_center: fil
ADD failed: file not found in build context or excluded by .dockerignore: stat irap_rule_center: fil
|
4月前
webpack——You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file.
webpack——You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file.
620 0
|
4月前
ESLint—— Failed to load config "standard" to extend from
ESLint—— Failed to load config "standard" to extend from
85 0
Invalid mapping pattern detected: /download/{{fileName}} ^Not allowed to nest variable c
Invalid mapping pattern detected: /download/{{fileName}} ^Not allowed to nest variable c
Warning: To load an ES module, set “type“: “module“ in the package.json or use the .mjs extension.
Warning: To load an ES module, set “type“: “module“ in the package.json or use the .mjs extension.
|
7月前
|
JavaScript
Parsing error: No Babel config file detected for 。。
Parsing error: No Babel config file detected for 。。
162 3
|
7月前
|
IDE 开发工具 Android开发
Couldn‘t get post build model. Module:UpdateService_0804.main Variant: debugOpen logcat panel fo
Couldn‘t get post build model. Module:UpdateService_0804.main Variant: debugOpen logcat panel fo
85 0
|
7月前
|
JavaScript 前端开发
ESLint—— Failed to load config “standard“ to extend from
ESLint—— Failed to load config “standard“ to extend from
198 0
|
7月前
Parsing error: No Babel config file detected for XXXXX
Parsing error: No Babel config file detected for XXXXX
|
JSON 数据格式
Required request parameter ‘name‘ for method parameter type String is not present 报错解决方法
Required request parameter ‘name‘ for method parameter type String is not present 报错解决方法
3770 0