工作中有个excel,其中一列是一组数字。数字的含义是商品的商品码。商品的状态有3种,1,2,3.需要连接到后台的oracle数据库,查询商品的状态,然后从这个excel表的商品码中筛选出1或者2的。然后保存在另外一个excel里。
整个代码是:
#coding:utf-8
import xlrd
import xlwt
import cx_Oracle
db=cx_Oracle.connect(‘用户名’,’密码’,’ip:1521/服务名’)
workbook = xlwt.Workbook(encoding = ‘ascii’)
worksheet = workbook.add_sheet(‘My Worksheet’)
data = xlrd.open_workbook(‘1.xls’)
table = data.sheets()[0]
nrows = table.nrows
for i in range(nrows):
rowi=unicode(table.row_values(i)[0])
sql=”select * from tskuplu where pluCode = ‘”+rowi+”‘ And IsActive = ‘1’ and YwStatus in (‘1′,’2’)”
cr=db.cursor()
cr.execute(sql)
rs=cr.fetchall()
if rs:
print rs[0][1],type(rs[0][1])
worksheet.write(i, 0, label = rs[0][1])
workbook.save(‘111.xls’)
cr.close()
db.close()
参考资料:
https://www.cnblogs.com/lhj588/archive/2012/01/06/2314181.html
http://python.jobbole.com/81244/
https://www.cnblogs.com/ylqmf/archive/2012/04/16/2451841.html