【轉載】python使用loggin和ConfigParser配置檔案中遇到的問題
原文連結:https://blog.csdn.net/weixin_39918285/article/details/79551104
問題一: 載入loggin配置檔案時出錯 UnicodeDecodeError
一開始logger.conf配置檔案的存檔格式為unicode,該檔案中有中文字元。
logging.config.fileConfig("logger.conf")
所以下面語句載入時,出現如下錯誤:
UnicodeDecodeError: 'gbk' codec can't decode byte 0xff in position 0: illegal multibyte sequence
將檔案存檔為 ANSI 模式後,問題解決。相關的文字編解碼問題,可以參考:
【總結】Python 2.x中常見字元編碼和解碼方面的錯誤及其解決辦法
問題二:loggin檔案解析過程中出錯: log.addHandler(handlers[hand]) KeyError: 'fileHandler #將root logger的日誌
在配置檔案中,handlers後面又備註資訊。
[logger_root] #root logger
level=DEBUG
handlers=consoleHandler,fileHandler #將root logger的日誌資訊輸出到檔案和控制檯,這個不能放在下
將其修改如下後,問題解決。
[logger_root] #root logger
level=DEBUG
#將root logger的日誌資訊輸出到檔案和控制檯,這個不能放在下
handlers=consoleHandler,fileHandler
問題三:用ConfigParser載入cfg.ini解析錯誤,
File "D:\ProgramData\Anaconda3\lib\configparser.py", line 1080, in _read
raise MissingSectionHeaderError(fpname, lineno, line)
configparser.MissingSectionHeaderError: File contains no section headers.
file: 'cfg.ini', line: 1
'\ufeff[filecfg]\n'
問題原因為cfg.ini在windows平臺上存的檔案編碼格式為utf-8。用
cf.read('cfg.ini',encoding='utf-8')
是不行的,必須使用 encoding='utf-8-sig' 才可以
---------------------
作者:weixin_39918285
來源:CSDN
原文:https://blog.csdn.net/weixin_39918285/article/details/79551104
版權宣告:本文為博主原創文章,轉載請附上博文連結!