python讀取txt文件
1、錯誤一
with open(path,'r') as f: for line in f: line = line.strip()
#
# 報錯: UnicodeDecodeError: 'gbk' codec can't decode byte 0xac in position 451428: illegal multibyte sequence
2、錯誤二
with open(path,encoding="UTF-8")
#
# 報錯: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc0 in position 278: invalid start byte
三、最好的辦法
with open(path, 'rb') as f:#使用二進制讀取 for line in f: #line的數據類型是bytes line = str(line) #將bytes類型轉換為str類型 line = line.strip()