使用xlrd读取包含中文和/或印地语字符的Excel xls文件 [英] Using xlrd to read Excel xls file containing Chinese and/or Hindi characters
问题描述
http://scienceoss. com/read-excel-files-from-python/comment-page-1/#comment-1051
通过上面的链接,我使用了此实用程序来读取XLS文件.如果XLS文件包含其他语言字符(例如中文或印地语),则不会正确输出它们.有解决方法吗?
From the above link, I used this utility to read an XLS file. If the XLS file contains different language characters like Chinese or Hindi, it does not output them correctly. Is there a workaround for this?
在谷歌搜索后,我发现了这一点:
After Googling, I found this:
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')
行中有一个错误:
But there is an error in the line book = open_workbook('dodgy.xls',encoding='cp1252')
:
TypeError: open_workbook() got an unexpected keyword argument 'encoding'
推荐答案
根据xlrd 模块文档,正确的参数是: encoding_override ="cp1252" ,而不是 encoding ="cp1252" .
According to the xlrd module documentation, the correct parameter is: encoding_override="cp1252" and not encoding="cp1252".
从导入xlrd模块的方式来看,您应该将函数调用为xlrd.open_workbook,但是在示例代码中,您可以直接使用该函数,就像您曾经使用过"from xlrd import *"一样.
From the way you are importing the xlrd module you should be calling the function as xlrd.open_workbook but in the example code you use the function directly, as if you had used "from xlrd import *".
这篇关于使用xlrd读取包含中文和/或印地语字符的Excel xls文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!