我正在编写一个 Python 程序来读取输出到文本文档中的 DOS 树命令。当我到达循环的第 533 次迭代时,Eclipse 给出了一个错误:
Traceback (most recent call last):
File "E:\Peter\Documents\Eclipse Workspace\MusicManagement\InputTest.py", line 24, in <module>
input = myfile.readline()
File "C:\Python33\lib\encodings\cp1252.py", line 23, in decode
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 3551: character maps to undefined
我已阅读其他帖子,将编码设置为 latin-1 并不能解决此问题,因为它UnicodeDecodeError
在另一个字符上返回 a,并且与尝试使用 utf-8 相同。
以下是代码:
import os
from Album import *
os.system("tree F:\\Music > tree.txt")
myfile = open('tree.txt')
myfile.readline()
myfile.readline()
myfile.readline()
albums = []
x = 0
while x < 533:
if not input: break
input = myfile.readline()
if len(input) < 14:
artist = input[4:-1]
elif input[13] != '-':
artist = input[4:-1]
else:
albums.append(Album(artist, input[15:-1], input[8:12]))
x += 1
for x in albums:
print(x.artist + ' - ' + x.title + ' (' + str(x.year) + ')')