【问题标题】:Unicode from xls to CSV从 xls 到 CSV 的 Unicode
【发布时间】:2014-02-13 16:08:41
【问题描述】:

我从 2 天以来一直在尝试找出以下将 xls 文件转换为 CSV 文件的代码中的确切错误 我的问题是输出 CSV 上的某些字符未正确编码(é、à...等)。我已经阅读了大量关于 SOF 的帖子,但我没有找到解决方案。我知道问题来自仅处理 Ascii 或 UTF-8 的 csv 模块,但我不知道如何处理它。 我也使用了替换模块unicodecsv,但没有成功。我知道这里有一些 unicode csv unicode example 的示例,但我不知道正确的使用方法。

我确定我的 xls 是在 utf_16_LE(工作簿编码)上编码的。

这是我在 SOF 上找到的代码。我尝试了很多修改,但没有成功。有人可以分辨出代码的哪一部分发生了变化。

#!/usr/bin/env python
# -*- coding: utf8 -*-
import xlrd
from os import sys
import csv 


def csv_from_excel(excel_file):

    workbook = xlrd.open_workbook(excel_file)
    print workbook.biff_version, workbook.codepage, workbook.encoding
    #test read of accent charactere
    rs = workbook.sheet_by_index(0)
    print rs.cell_value(1,0)

    all_worksheets = workbook.sheet_names()
    for worksheet_name in all_worksheets:
        worksheet = workbook.sheet_by_name(worksheet_name)
        your_csv_file = open(''.join([worksheet_name,'.csv']), 'wb')

        class ExcelFr(csv.excel):
        #Separateur de champ
            delimiter = ";"

        csv.register_dialect('excel-fr', ExcelFr())

        wr = csv.writer(your_csv_file,'excel-fr', quoting=csv.QUOTE_ALL)

        for rownum in xrange(worksheet.nrows):
            wr.writerow([unicode(entry).encode("utf-8") for entry in worksheet.row_values(rownum)])

        your_csv_file.close()

#if __name__ == "__main__":
#    csv_from_excel(sys.argv[1])

csv_from_excel("source-2014-02-12.xls")

编辑:新代码:仅转换第一张纸(我不需要任何纸)。

#!/usr/bin/env python
# -*- coding: utf8 -*-
import xlrd
import unicodecsv
import codecs

def csv_from_excel(excel_file):

    wb = xlrd.open_workbook(excel_file)
    print wb.biff_version, wb.codepage, wb.encoding
    sh = wb.sheet_by_name('Feuil1')
    print sh.row_values(8)
    #your_csv_file = open('your_csv_file.csv', 'wb')
    your_csv_file = codecs.open('your_csv_file.csv','wb')

    class ExcelFr(unicodecsv.excel):
        #Separateur de champ
        delimiter = ";"

    unicodecsv.register_dialect('excel-fr', ExcelFr())

    wr = unicodecsv.writer(your_csv_file,'excel-fr',encoding='utf-8', quoting=unicodecsv.QUOTE_ALL)

    for rownum in xrange(sh.nrows):
        wr.writerow(sh.row_values(rownum))
        #wr.writerow([unicode(entry).encode("utf-8") for entry in sh.row_values(rownum)])

    your_csv_file.close()

csv_from_excel("source-2014-02-13.xls")


reader = unicodecsv.reader("your_csv_file.csv")
print reader.encoding

输出:

80 1200 utf_16_le [u'Chaise de Massage ergonomique pliante', u'Facile \xe0 monter et ajustable \xe0 tout gabarit et pour tout traitement du haut du corps comme la t\xeate, le dos, les \xe9paules et les bras。 Le soutien pour la t\xeate est amovible et ajustable comme l\u2019assise et l\u2019accoudoir。 Le Massage sur chaise est une mani\xe8re tr\xe8s efficace de stimuler la circular du sang, de l\u2019\xe9nergie et permet au corps de retrouver un certain \xe9quilibre。注意 que la chaise peut \xe9galement \xeatre utilis\xe9e comme chaise de tatouage。 ', u'Fauteuil de Massage blanc, pliant et facile \xe0 transporter..... 等等 utf-8

如你所见,有像我说的'\xe0'或'\u2019'这样的字符

我还是不明白编码方面的所有混乱!

【问题讨论】:

  • 那是 Python 2 还是 3?

标签: python excel csv unicode encoding


【解决方案1】:

在你的情况下,这是错误的:

your_csv_file = open(''.join([worksheet_name,'.csv']), 'wb')

标准 Python open() 函数会打开二进制文件,因此您需要确保自己正确编码数据。您应该导入 codecs 模块并使用:

your_csv_file = codecs.open(''.join([worksheet_name,'.csv']), 'w', 'utf-8')

我同意你的观点,不过unicode(entry).encode("utf-8") 应该具有相同的效果。

如果我的建议没有帮助,那么您需要告诉我们您认为“某些字符编码不正确”的确切原因。

【讨论】:

【解决方案2】:

好像你只是不明白你在看什么

打开idle进入

print u" mani\xe8re tr\xe8s" \x## 只是一个没有 ascii 表示的十六进制数字,

print u"l\u2019assise et l\u2019accoudoir" 将证明 \u#### 只是一个没有 reperesentation 的 unicode 字符

【讨论】:

  • 我在写法的过程中已经尝试了很多编码,但仍然被阻塞。
  • Thx Joran,其实我知道这个十六进制数字,但不明白这就是为什么当我用 excel 打开它时它会显示这些字符。我将在我的服务器上进行一些测试,看看我读取 csv 文件时的样子。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-02-29
  • 2012-04-10
  • 2010-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-17
相关资源
最近更新 更多