您的位置:首页 > 编程语言 > Python开发

提取EXCEL表格中特定列内容保存至txt文档

2014-05-10 11:05 357 查看
不能用wps文件

引用文档中有中文,开头声明 # coding=gbk

写入文件时有中文,需要加入以下代码

import sys
reload(sys)
sys.setdefaultencoding('utf-8')

完整代码:
# coding=gbk
import sys
import xlrd
import time
reload(sys)
sys.setdefaultencoding('utf-8')
book = xlrd.open_workbook(r'C:\Users\Administrator\Desktop\510\123.xls')
sheet = book.sheet_by_index(0)
countRow = sheet.nrows
start = time.time()
f=open(r'C:\Users\Administrator\Desktop\510\read.txt','w')
for i in range(1,countRow):
listRow = sheet.row(i)
getName = listRow[1].value
getBigindex = listRow[7].value
getSmallindex = listRow[8].value
getID = listRow[0].value
ss='%s %d %d %d\n' % (getName,getBigindex,getSmallindex,getID)
f.write(ss)
f.close()
c =time.time()-start
print '程序运行耗时:%.2f' % c
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python