python读取xls文件中文,使用xlrd读取包含中文和/或印地语字符的Excel 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

But there is an error in the line book = open_workbook('dodgy.xls',encoding='cp1252'):

TypeError: open_workbook() got an unexpected keyword argument 'encoding'

解决方案

According to the xlrd module documentation, the correct parameter is: encoding_override="cp1252" and not encoding="cp1252".

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 *".

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值