我正在尝试从文件中拆分行并将它们放入 excel 文件(xlsx)中。根据 PS PAD,文件的编码是 'cp1250'。所以要在 xlsx 文件中有正确的字符,我正在从 cp1250 解码这些行 -line = line.decode("cp1250")
问题是来自 12000 的 cca 3000 行返回此错误:
'charmap' codec can't decode byte 0x81 in position 25: character maps to <undefined>
所以接下来我尝试解码(“utf-8”),我不知道为什么,但它更好。只有 330 行返回错误:
'utf8' codec can't decode byte 0x8e in position 0: invalid start byte
你们知道我做错了什么吗?
编辑:当行包含“Ž”或“Š”时,通常会发生错误
这是代码:(在 py 文件的顶部,我放了“# - - coding: utf-8 - -”)
def toXls(file):
workbook = xlsxwriter.Workbook(file)
worksheet = workbook.add_worksheet()
a=0
with open("filtrovane.txt") as f:
x=0
for line in f:
try:
line = line[:-1].decode("utf-8") """It should be "cp1250" according to PSPAD editor"""
# line = line.encode("ISO 8859-2")
splitted = line.split("::")
if len(splitted)==7:
try:
a=a+1
worksheet.write(a,0,splitted[0])
worksheet.write(a,1,splitted[1])
worksheet.write(a,2,splitted[2])
worksheet.write(a,3,splitted[3])
worksheet.write(a,4,splitted[4])
worksheet.write(a,5,splitted[5])
worksheet.write(a,6,splitted[6])
except Exception as e:
print "!!"+line+" "+a + e
except Exception as e:
print e
x=x+1
print x
workbook.close()