解决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())