python中如何将xls文件的内容写到TXT文件中,大神搭救

小木桥 发布于 2016/03/19 16:02
阅读 446
收藏 0

【DevOps必读】产品经理与程序员之间如何破局?>>>

#coding:utf8
import xlrd
from xlrd import open_workbook
excel=open_workbook(r"201503new.xls")
num=excel.nsheets
mysheet=excel.sheet_by_index(num-1)
print mysheet.nrows,mysheet.ncols
f=open(r"F:\javadevelop\dianxin\3.txt","ab")
for i in range(0,mysheet.nrows):
    tmp=""
    lists=[]
    for j in range(0,mysheet.ncols):
        if mysheet.cell(i,j).value!=None:
            tmp+=str(mysheet.cell(i,j).value)+u"\t"
    f.write(tmp.encode('utf8'))  

错误提示:说是编码问题,如何改,请教一下    

Traceback (most recent call last):

  File "F:\javadevelop\dianxin\readExcel.py", line 14, in <module>
    tmp+=str(mysheet.cell(i,j).value)+u"\t"
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3: ordinal not in range(128)

加载中
返回顶部
顶部