Python编写脚本在windows和linux上面出现的错误

windows上运行错误信息如下:

config.read('config.ini')
File "D:\Python27\lib\ConfigParser.py", line 305, in read
self._read(fp, filename)
File "D:\Python27\lib\ConfigParser.py", line 512, in _read
raise MissingSectionHeaderError(fpname, lineno, line)
MissingSectionHeaderError: File contains no section headers.
file: config.ini, line: 1
'\xef\xbb\xbf[userinfo]\n'

作如下操作:

content = open('config.ini').read()
#Window下用记事本打开配置文件并修改保存后,编码为UNICODE或UTF-8的文件的文件头
#会被相应的加上\xff\xfe(\xff\xfe)或\xef\xbb\xbf,然后再传递给ConfigParser解析的时候会出错
#,因此解析之前,先替换掉
content = re.sub(r"\xfe\xff","", content)
content = re.sub(r"\xff\xfe","", content)
content = re.sub(r"\xef\xbb\xbf","", content)
open('config.ini', 'w').write(content)


最后问题解决!


你可能感兴趣的:(Python编写脚本在windows和linux上面出现的错误)