今天使用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

ConfigParser.MissingSectionHeaderError: File contains no section headers.的更多相关文章

  1. python ConfigParser读取配置文件,及解决报错(去掉BOM)ConfigParser.MissingSectionHeaderError: File contains no section headers的方法

    先说一下在读取配置文件时报错的问题--ConfigParser.MissingSectionHeaderError: File contains no section headers 问题描述: 在练 ...

  2. raise missingsectionheadererror:file containe no section headers问题解决

    本人亲测,遇到这个问题,就换到管理员方式运行命令 因为太小白,所以这次重新装包的时候切换到D盘了,想着省一点儿C盘内存,结果,每次pip install安装的时候都是这个问题,中间还有什么反序列失败, ...

  3. 【问题解决方案】CentOS7替换yum的问题:使用yum makecache出现File contains no section headers

    参考链接 CentOS7替换yum的问题:使用yum时出现File contains no section headers centos安装网络repo源及错误说明 一.centos替换yum的步骤 ...

  4. CentOS7使用yum时File contains no section headers.解决办法

    本文转载于  https://blog.csdn.net/trokey/article/details/84908838 安装好CenOS7后,自带的yum不能直接使用,使用会出现如下问题: 原因是没 ...

  5. ConfigParser读取记事本、notepad++修改后的配置文件会出现:ConfigParser.MissingSectionHeaderError

    使用ConfigParser来读取配置文件,经常会发现经过记事本.notepad++修改后的配置文件读取时出现下面的问题: ConfigParser.MissingSectionHeaderError ...

  6. ConfigParser读取配置文件时报错:ConfigParser.MissingSectionHeaderError

    使用ConfigParser来读取配置文件,经常会发现经过记事本.notepad++修改后的配置文件读取时出现下面的问题: ConfigParser.MissingSectionHeaderError ...

  7. Android7.0后JNI库必须保留Section Headers

    此修改在官网的描述如下: Each ELF file has additional information contained in the section headers. These header ...

  8. [CSS3] Use Sticky Positioning for Section Headers

    We can take advantage of sticky positioning to keep a section header at the top of the page while th ...

  9. mdk keil 指定变量、函数存储位置,使用 Scatter-Loading Description File, __attribute__(("section“))

    0. 数据类型说明 主要包括4类: Code (inc. data) ,属于RO,也就是写的函数代码(包括代码中的变量) RO Data , 属于RO,使用const修饰的变量. RW Data, 属 ...

随机推荐

  1. AC自动机最好讲解

    http://www.cs.uku.fi/~kilpelai/BSA05/lectures/slides04.pdf

  2. NOIP2014提高组 酱油记

    NOIP考到哪里我就写到哪里好了. 2014/10/12 初赛 下午两点半开始考,我两点就到了.然后看到了QYL,NYZ,CZR等大神,先Orz了再说. 考试开始前,发现考场竟然没几个我认识的,不是按 ...

  3. css属性选择器

    1.E[att] 选择具有attr属性的E元素.2.E[att="val"] 选择具有att属性且属性值等于val的E元素3.E[att~="val"] 选择具 ...

  4. SqlServer索引及优化详解

    实际上,您可以把索引理解为一种特殊的目录.微软的SQL SERVER提供了两种索引:聚集索引(clustered index,也称聚类索引.簇集索引)和非聚集索引(nonclustered index ...

  5. 1427. SMS

    http://acm.timus.ru/problem.aspx?space=1&num=1427 没想到这道题错了N遍  细节很重要呀 代码: #include<iostream> ...

  6. maven3.04管理jetty9.2.10启动web项目

    在pom.xml文件中添加如下: <build>    <pluginManagement>        <plugins>            <plu ...

  7. hive的基本操作

    1.创建表 First, create a table with tab-delimited text file format: (1)CREATE TABLE u_data ( userid INT ...

  8. c#中各类日期的计算方法,收藏

    DateTime startWeek = dt.AddDays(1 - Convert.ToInt32(dt.DayOfWeek.ToString("d")));  //本周周一D ...

  9. BZOJ 3110 树套树 && 永久化标记

    感觉树套树是个非常高深的数据结构.从来没写过 #include <iostream> #include <cstdio> #include <algorithm> ...

  10. POJ 1637 Sightseeing tour (混合图欧拉路判定)

    Sightseeing tour Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6986   Accepted: 2901 ...