在Python中使用encode('utf-8')从Excel读取字符串的缺点

2 投票
2 回答
3540 浏览
提问于 2025-04-17 04:12

我正在从一个很大的Excel表格中读取数据,在这个过程中,我会对数据进行重新格式化和写入,使用的结构大致是这样的:

book = open_workbook('file.xls')
sheettwo = book.sheet_by_index(1)
out = open('output.file', 'w')
for i in range(sheettwo.nrows):
     z = i + 1
     toprint = """formatting of the data im writing. important stuff is to the right -> """ + str(sheettwo.cell(z,y).value) + """ more formatting! """ + str(sheettwo.cell(z,x).value.encode('utf-8')) + """ and done"""
     out.write(toprint)
     out.write("\n")

在这里,x和y是任意的单元格,其中x比较特殊,包含utf-8字符。

到目前为止,我只在那些我知道会出错的单元格中使用了.encode('utf-8'),或者是我预见到如果不使用utf-8会出错的地方。

我的问题基本上是这样的:如果在所有单元格上都使用.encode('utf-8'),即使有些地方其实不需要,这样做有没有什么坏处?效率不是问题。主要的问题是,即使在不该有utf-8字符的地方,它也能正常工作。如果我在每个读取的单元格上都加上“.encode('utf-8')”而不会出现错误,那我可能会选择这样做。

2 个回答

0

这个回答其实只是对接受的答案的一些简单评论,但需要更好的格式来展示,而不是用SO评论的方式。

(1) 避免出现SO的水平滚动条,这样能提高人们阅读你代码的可能性。试着把你的代码行换行,比如:

toprint = u"".join([
    u"formatting of the data im writing. "
    u"important stuff is to the right -> ",
    unicode(sheettwo.cell(z,y).value),
    u" more formatting! ",
    unicode(sheettwo.cell(z,x).value),
    u" and done\n"
    ])
out.write(toprint.encode('UTF-8'))

(2) 你可能在用 unicode() 来把浮点数和整数转换成unicode格式;但如果值已经是unicode格式,它就没什么作用。要知道,unicode()str() 一样,对于浮点数只提供12位的精度:

>>> unicode(123456.78901234567)
u'123456.789012'

如果这让你觉得麻烦,你可以试试这样的做法:

>>> def full_precision(x):
>>> ... return unicode(repr(x) if isinstance(x, float) else x)
>>> ...
>>> full_precision(u'\u0400')
u'\u0400'
>>> full_precision(1234)
u'1234'
>>> full_precision(123456.78901234567)
u'123456.78901234567'

(3) xlrd 会在需要的时候动态创建 Cell 对象。

sheettwo.cell(z,y).value # slower
sheettwo.cell_value(z,y) # faster
4

XLRD 文档中明确说明:“从 Excel 97 开始,Excel 表格中的文本以 Unicode 格式存储。”因为你很可能在处理 97 之后的文件,所以这些文件里本身就包含 Unicode 字符。为了确保在 Python 中正确处理这些单元格的内容,你需要保持它们的 Unicode 格式,而不是把它们转换成 ASCII(这通常是用 str() 函数来做的)。请使用下面的代码:

book = open_workbook('file.xls')
sheettwo = book.sheet_by_index(1)
#Make sure your writing Unicode encoded in UTF-8
out = open('output.file', 'w')
for i in range(sheettwo.nrows):
    z = i + 1
    toprint = u"formatting of the data im writing. important stuff is to the right -> " + unicode(sheettwo.cell(z,y).value) + u" more formatting! " + unicode(sheettwo.cell(z,x).value) + u" and done\n"
    out.write(toprint.encode('UTF-8'))

撰写回答