首页 > 解决方案 > Revit.ini file - MissingSectionHeaderError: File contains no section headers. file: Revit.ini, line: 1 '\xff\xfe\r\x00\n'

问题描述

im trying to edit my Revit.ini File with python however I continue getting the error below. I've been banging my head against the wall for the better portion of the day. Any help is appreciated in this.

I've been working with ConfigParser on python 2.7 and going through their docs here since they seem to be the way to go when trying to work with *.ini files. When i create my own *.ini file to test stuff on, everything works however when i try to run my test on this other *.ini, i get an error.

my code so far

import ConfigParser
config = ConfigParser.RawConfigParser()
config.read('Revit.ini')

my error

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "C:\Python27\lib\ConfigParser.py", line 305, in read
    self._read(fp, filename)
  File "C:\Python27\lib\ConfigParser.py", line 512, in _read
    raise MissingSectionHeaderError(fpname, lineno, line)
MissingSectionHeaderError: File contains no section headers.
file: Revit.ini, line: 1
'\xff\xfe\r\x00\n'

Any and all help is appreciated. I think it has something to do with the UTF encoding(which im not at all familiar with yet. i intend to do some reading on it) which then means i probably have to decode it and then re-encode it which i also need to read up on what to do.

标签: pythonpython-2.7configparserrevit

解决方案


我认为这与UTF编码有关

是的,它确实。错误消息将文件的前两个字节显示为\xff\xfe,这是包含编码为 UTF-16LE(小端序)的 Unicode 字符的文本文件的字节顺序标记 (BOM)。配置解析器在理解此类文件时需要一点帮助,因此我们可以请求io模块提供帮助:

config = configparser.ConfigParser()
with io.open('revit.ini', mode='r', encoding='utf-16') as fp:
    config.read_file(fp)

请注意,以上内容适用于 Python_3。Python_2 距离生命周期结束(2020 年 1 月)还有不到六个月的时间,在这之后,预计很多软件包都会放弃对它的支持,所以你真的应该考虑过渡到 Python_3。


推荐阅读