问题:
>>> import configparser
>>> parser=configparser.ConfigParser()
>>> con=parser.read("Chinese.ini")
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
con=parser.read("Chinese.ini")
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\configparser.py", line 697, in read
self._read(fp, filename)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\configparser.py", line 1015, in _read
for lineno, line in enumerate(fp, start=1):
UnicodeDecodeError: 'gbk' codec can't decode byte 0xad in position 21: illegal multibyte sequence
原因关键字:BOM,\ufeff
解决:
>>> con=parser.read("Chinese.ini",encoding="utf-8-sig")
参考资料来源:今天忘记记录,下次再记录算了。
***************************************************************************************************************************
问题:
>>> for sect in parser.sections():
print("==============={}==============".format(sect))
for opt in parser.options(sect):
s=parser.get(sect,opt)
print("{}={}".format(opt,s))
Traceback (most recent call last):
File "<pyshell#40>", line 4, in <module>
s=parser.get(sect,opt)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\configparser.py", line 800, in get
d)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\configparser.py", line 394, in before_get
self._interpolate_some(parser, option, L, value, section, defaults, 1)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\configparser.py", line 444, in _interpolate_some
"found: %r" % (rest,))
configparser.InterpolationSyntaxError: '%' must be followed by '%' or '(', found: '%d 人的未限制點播房間)'
原因关键字:特殊符号%,get
改进:
>>> for sect in parser.sections():
print("==============={}==============".format(sect))
for opt in parser.options(sect):
try:
s=parser.get(sect,opt)
except:
print("{}-->{} error! passed..".format(sect,opt))
print("{}={}".format(opt,s))
最终:
>>> parser=configparser.RawConfigParser()
>>> parser.read("Chinese.ini",encoding="utf-8-sig")
['Chinese.ini']
>>> for sect in parser.sections():
print("==============={}==============".format(sect))
for opt in parser.options(sect):
s=parser.get(sect,opt)
print("{}={}".format(opt,s))
参考资料来源:--