1. 程式人生 > 其它 >‘gbk‘ codec can‘t decode byte 0xff in position 0: illegal multibyte sequence 怎麼辦

‘gbk‘ codec can‘t decode byte 0xff in position 0: illegal multibyte sequence 怎麼辦

技術標籤:Python基礎語法

開啟txt檔案時,出現

'gbk' codec can't decode byte 0xff in position 0: illegal multibyte sequence

在python前面,加

#-*- coding : utf-8 -*-
# coding: utf-8

結果不行。
把讀取格式改成

word=open('d:/ex/china.txt','r',encoding='"unicode_escape"')

spyder裡處理的結果是

 'ÿþ\n\x00\n\x00I\x00 \x00l\x00o\x00v\x00e\x00 \x00C\x00h\x00i\x00n\x00a\x00.\x00\n\x00\n\x00'

Visual Studio Code的結果是
在這裡插入圖片描述
不是自己理想的樣子。

後來想到這個是直接在powershell裡建的檔案,不是在notepad++建的,於是重新在notepad++新建txt檔案,讀取果然沒問題。