在我的项目中,需要根据excel的表头创建一个数据库,这时候把它们导出成txt的一列会方便很多。这种事情用python实在是太爽快了,我是这样做的:
# -*- coding: utf-8 -*-
import xlrd
# 通过xlrd从xls的绝对路径得到workbook,比poi方便多了
workbook = xlrd.open_workbook('C:\Users\mac\Documents\yindaTech\qiandao.xls')
# 得到第1张表
sheet = workbook.sheet_by_index(0)
# 得到第3行内容的集合
row = sheet.row_values(2)
# 打印一下第3行的长度(纯属娱乐)
print "数组的长度是:%s" % len(row)
# 用w模式创建一个用于输出的文件并打开,如已存在会覆盖
fileWrite = open("C:\Users\mac\Documents\yindaTech\output\excelRow.txt", "w")
for i in row:
# 一定要转成utf-8才能输出
output = i.encode('utf-8') + '\n'
fileWrite.write(output)
# 写完了要关闭,好习惯,不然其他进程可能就不能改写了
fileWrite.close()

另外,推荐一个网址,就一个例子,但对于python-excel说得很全了。
http://www.jb51.net/article/60510.htm
网友评论