python - 编码错误 - xlsxwriter - Python

标签 python excel encoding utf-8 xlsxwriter

我正在尝试从文件中拆分行并将它们放入 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()

最佳答案

XlsxWriter docs/repo 中有两个示例显示如何阅读 UTF-8Shift JIS文件并将它们转换为 xlsx 文件。

它应该对 cp1250 起作用。 .

关于python - 编码错误 - xlsxwriter - Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25548921/

相关文章:

java - 为什么 java/javascript/python 强制在方法名称后使用 (),即使它不带参数?

python - 如何在 CKAN 中添加新页面?

r - excel.link R 包强制 Excel 自动计算

c# - 如何在 C# 中获得正确的 HTML 编码?

c++ - 如何使用 Qt 处理 json 中编码的西里尔字母数据?

python - 在 Python 中将分类数据转换为数值数据

python - 为什么需要在循环内创建可变随机函数才能正常工作?

python 3 : get 2nd to last index of occurrence in string

Excel 反向递归(?) 搜索

excel - 如何使用 VSTO 获取 Excel 中工作表的 CodeName