我正在尝试创建一个web应用程序,允许用户上传一个.xls文件,然后将该uploaded.xls文件输入到我的程序中,该程序读取并解析该文件。我目前正在Web.py框架上使用Python2.7。
但是,我对Excel文件的utf-8编码有问题.这个方法似乎只适用于.txt & .csv文件,但是当我尝试图片或.pdf时,它们不起作用,所以我不确定库中内置的web.py是否只是不支持web.py文件。当我上传一个Excel文件时,它只会发出不可读的内容,如下所示:
♠☺☻☺☻╒═╒▲.←►+,∙0░☺H↨P♂X♀☻Σ♦♥♫♂♂♂♂▲►☺▲♂工作表♥☺
这是我的代码:
class index:
def POST(self):
x = web.input(calendar_file={}, ref_id='')
if x:
ref_id = (x.ref_id if x.ref_id else "")
filepath=x.calendar_file.filename # replaces the windows-style slashes with linux ones.
fn=filepath.split('/')[-1] # splits the and chooses the last part (the filename
filename = "%s/Users/jl98567/Documents/xMatters_calendar_app/test/" + fn
fullpath = os.path.join('c:', filename % (ref_id))
content = x["calendar_file"].file.read()
with open(fullpath, 'w') as f_out:
if not f_out:
raise Exception("Unable to open %s for writing. " % (fullpath))
f_out.write(content)
print x['calendar_file'].value
raise web.seeother('/upload?ref_id=%s&filename=%s' % (ref_id, filename))现在,当我试图编码:
print x['calendar_file'].value.encode('utf-8')我得到以下错误:
at / 'ascii‘编解码器无法解码0位置的字节0xd0 :序数不在范围内(128)
奇怪的是,我知道将其编码为utf-8可以在我的应用程序上工作,该应用程序不是基于web的,也不是使用web.py文件上传方法的。所以我似乎看不出这里有什么问题。
例如:
content = str(sheet.cell(row,0).value.encode('utf8'))使用xlrd和xlwt python方法可以很好地工作。
有什么建议吗?
非常感谢!
发布于 2014-06-05 21:26:16
print unicode(x['calendar_file'].value, 'utf-8')https://stackoverflow.com/questions/21562204
复制相似问题