UnicodeDecodeError: 'gbk' codec can't decode byte

运行 "Learn Python the Hard Way" 里的代码时出现的报错。代码:

1.  from sys import argv
2.  from os.path import exists                              
3.  
4.  script, from_file, to_file = argv
5.  
6.  print(f"Copying from {from_file} to {to_file}")
7.  
8.  # we could do these two on one line, how?
9.  in_file = open(from_file)
10. indata = in_file.read()
11.
12. print(f"The input file is {len(indata)} bytes long")
13.
14. print(f"Does the output file exsit? {exists(to_file)}")
15. print("Ready, hit RETURN to continue, CTRL-C to abort.")
16. input()
17. 
18. out_file = open(to_file, 'w')
19. out_file.write(indata)
20. 
21. print("Alright, all done.")
22. 
23. out_file.close()
24. in_file.close() 

照抄书本在 Terminal 上输入的命令:
$ echo "This is a test file." > test.txt
$ cat test.txt
This is a test file.
$ python ex17.py test.txt new_file.txt

下面是 Terminal 给的提示:
Copying from test.txt to new_file.txt
Traback ( most recent call last):
File "ex17.py", line 10, in
indata = in_file.read()
UnicodeDecodeError: 'gbk' codec can't decode byte 0xff in position 0: illegal multibyte sequence

尝试把 'gbk' 编码改为 'UTF-8':
9. in_file = open(from_file, 'r', encoding = 'UTF-8')

Terminal 给出的提示

Copying from test.txt to new_file.txt
Traceback (most recent call last):
File "ex17.py", line 10, in
indata = in_file.read()
File "C:\Python36\lib\codecs.py", line 321, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte

求解:是什么导致这个 Error ?以及如何解决?

UnicodeDecodeError 解码错误,请先确认源文件的编码是什么,然后用对应的编码解码。如果是gb2312,encoding='gb18030'试试,
如果编码确认无误,还出现解码错误,那么一定是文件出现无法解码的乱码。此时1:去除文件中的乱码 ; 2:以字节方式读入,然后decode时
传入errors='ignore'忽略解码错误即可。强调一下,一定要先确认编码无误。

编码不对,改一下编码

import math
import codecs
xy_coordinate = []  # 转换后的XY坐标集
def millerToXY (lng, lat):
    """
    经纬度转换为平面坐标系中的x,y 利用米勒坐标系
    :param lng: 经度
    :param lat: 纬度
    :return:
    """
    L = 6381372*math.pi*2
    W = L
    H = L/2
    mill = 2.3
    x = lng*math.pi/180
    y = lat*math.pi/180
    y = 1.25*math.log(math.tan(0.25*math.pi+0.4*y))
    x = (W/2)+(W/(2*math.pi))*x
    y = (H/2)-(H/(2*mill))*y
    xy_coordinate.append((int(round(x)),int(round(y))))
    return xy_coordinate

f = codecs.open('C:/Users/zcy98/Desktop/City.xlsx',mode = 'r',encoding = 'utf-8')
line = f.readline()
codes = []
while line:
    a = line.strip('\n')
    x = a[1]
    y = a[2]
    codes.append(millerToXY(x,y))
    line = f.readline
f.close()
for i in codes:
    print(i)