python - 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.
解决方案
我认为这与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。
推荐阅读
- javascript - 迭代 Handlesbars.js 中的对象数组
- postgresql - 有没有办法阻止此错误“从“client_api_key”中选择 *,其中“api_key”= $1 限制 $2 - 连接意外终止”
- elasticsearch - 是否可以在零停机时间的情况下更新 Elasticsearch 中的索引?
- java - 将 Fortify Audit Workbench 自定义设置转换为命令行脚本
- c - 可见性声明会影响使用(而不是编译)函数吗?
- docker - 就像在 docker 中一样在 Helm 中公开卷
- javascript - 如果为真则反跳 500 毫秒,否则执行
- substrate - Substrate Build a dApp Tutorial - TypeError: Cannot read property 'query' of undefined
- arm64 - 开始在 arm64 上运行时出现 headless_shell 分段错误
- javascript - 破坏反应道具中的嵌套对象出错