解決Python讀取文件時出現UnicodeDecodeError: 'gbk' codec can't decode byte...


用Python在讀取某個html文件時會遇到下面問題:

出問題的代碼:

1 if __name__ == '__main__':
2     fileHandler = open('../report.html', mode='r')
3 
4     report_lines = fileHandler.readlines()
5     for line in report_lines:
6         print(line.rstrip())

修改方式是在open方法指定參數encoding='UTF-8':

if __name__ == '__main__':
    fileHandler = open('../report.html', mode='r', encoding='UTF-8')

    report_lines = fileHandler.readlines()
    for line in report_lines:
        print(line.rstrip())

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM