http://scienceoss.com/read-excel-files-from-python/comment-page-1/#comment-1051
在上面的链接中,我使用这个实用程序读取了一个 XLS 文件。如果 XLS 文件包含不同的语言字符,如中文或印地语,则无法正确输出它们。有解决办法吗?
谷歌搜索后,我发现了这个:
import xlrd
def upload_xls(dir,file,request):
try:
global msg
global row_num
row_num = []
header_arr = []
global file_path
file_path = dir
#reader = csv.reader(open(file), delimiter='#', quotechar='"')
book = xlrd.open_workbook('dodgy.xls',encoding='cp1252') ##To specify UTF8-encoding
wb.sheet_names()
sh = wb.sheet_by_index(0)
valid_xl_format = 0
invalid_xl_format = 0
except:
print "Error
但是book = open_workbook('dodgy.xls',encoding='cp1252')
这一行有错误:
TypeError: open_workbook() got an unexpected keyword argument 'encoding'
最佳答案
[dis]claimer:我是 xlrd 的作者。
If the xls contains different language characters like chine or hindi.It does not output the exact wordings.Is there a work around for this..
encoding_override 参数(如文档中所述)仅用于旧文件(由早于 Excel 97(即 1997 年)的 Excel 生成),并且仅在内部记录的“代码页”丢失或不正确时使用。
注意:带有中文字符的旧文件:使用“cp1252”覆盖肯定会引发异常。
注意:带有“印地语”(天城文?)字符的旧文件:不太可能……据我所知,任何 ISCII 脚本都没有官方支持的代码页,我也没有听说过任何非官方的。欢迎提供有关此主题和/或示例文件的任何信息。
Excel 97 及更高版本以(有效)UTF-16LE 格式记录所有文本数据。如果文件是有效的 Excel-97 或更高版本文件,则忽略 encoding_override。
无论生成文件的 Excel 版本是什么,(如文档所述)xlrd 都会返回 unicode 字符串。您的问题很可能与您如何显示或转换这些 unicode 字符串有关。
如需进一步帮助,请编辑您的问题以显示实际输出的示例以及“确切措辞”。
关于python - 使用 xlrd 读取包含中文和/或印地语字符的 Excel xls 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3511743/