最近在为公司写一个脚本,主要目的是抓取已有的一个excel表数据,按一定格式另存在其他表上,主要这个格式很麻烦,其次数据太多,最多的一个表有四十多万数据,加起来一百多万。
作者:学要
链接:https://www.zhihu.com/question/24651024/answer/134760930
来源:知乎
著作权归作者所有,转载请联系作者获得授权。
#coding:utf-8
#使用的库
import csv,os,sys
import xlwt,xlrd
from xlrd import open_workbook
from xlutils.copy import copy
#为数组获得空间
Azimuth=[None]*1000000
Elevation=[None]*1000000
qiangdu=[None]*1000000
chushishuju='data_103717.xlsx'
def cn0():
data = xlrd.open_workbook(chushishuju)
table = data.sheets()[0]
nrows = table.nrows
ncols = table.ncols
#rows=table.row_values(3)
#print nrows
# print table.cell(1,1).value
c=1
for a in range (0,nrows-1):
#for b in range (1,ncols-1):
try:
row=table.cell(a,4).value
#print a
# row = row.replace('\r','').replace('\n','').replace('\t','')
qiangdu[c]=int(row)
c+=1
#print c
#print Azimuth[a]
except:
continue
table = data.sheets()[1]
nrows = table.nrows
ncols = table.ncols
# c+=1
#print c
for a in range (0,nrows-1):
#for b in range (1,ncols-1):
try:
row=table.cell(a,4).value
#print row
# print c
# row = row.replace('\r','').replace('\n','').replace('\t','')
qiangdu[c]=int(row)
c+=1
#print qiangdu[c]
except:
continue
def hengzhou():
data = xlrd.open_workbook(chushishuju)
table = data.sheets()[0]
nrows = table.nrows
ncols = table.ncols
#rows=table.row_values(3)
#print nrows
# print table.cell(1,1).value
c=1
for a in range (0,nrows-1):
#for b in range (1,ncols-1):
try:
row=table.cell(a,3).value
#print row
# row = row.replace('\r','').replace('\n','').replace('\t','')
Azimuth[c]=int(row)
c+=1
#print c
#print Azimuth[a]
except:
continue
table = data.sheets()[1]
nrows = table.nrows
#print nrows
ncols = table.ncols
# c+=1
#print c
for a in range (0,nrows-1):
#for b in range (1,ncols-1):
try:
row=table.cell(a,3).value
#print row,a
# row = row.replace('\r','').replace('\n','').replace('\t','')
Azimuth[c]=int(row)
# print Azimuth[c]
# print c
c+=1
#print c
#print Azimuth[c]
except:
continue
#print c
def zongzhou():
data = xlrd.open_workbook(chushishuju)
table = data.sheets()[0]
nrows = table.nrows
ncols = table.ncols
#rows=table.row_values(3)
#print nrows
# print table.cell(1,1).value
c=1
for a in range (0,nrows-1):
#for b in range (1,ncols-1):
try:
row=table.cell(a,2).value
#print a
# row = row.replace('\r','').replace('\n','').replace('\t','')
Elevation[c]=int(row)
c+=1
# print c
# print Azimuth[a]
except:
continue
table = data.sheets()[1]
nrows = table.nrows
ncols = table.ncols
#c+=1
for a in range (0,nrows-1):
#for b in range (1,ncols-1):
try:
row=table.cell(a,2).value
#print a
# row = row.replace('\r','').replace('\n','').replace('\t','')
Elevation[c]=int(row)
c+=1
except:
continue
if __name__=="__main__":
hengzhou()
zongzhou()
cn0()
outfile='test1.xls'
print Elevation[10000],Azimuth[10000],qiangdu[10000]
for a in range (1,1000000):
#print qiangdu[a],Azimuth[a],Elevation[a]
#print c
filename = xlwt.Workbook ()
sheet = filename.add_sheet('name11')
rb = open_workbook(outfile)
wb = copy(rb)
sheet = wb.get_sheet(0)
# sheet.write((19-(int((Azimuth[a])/5))),2,Azimuth[a])
#print (19-(int((Azimuth[a])5)))
if Azimuth[a]>179:
sheet.write((18-(Elevation[a]/5)),((Azimuth[a])/5-35),qiangdu[a])
#sheet.write((19-(Elevation[a]/5)),((Azimuth[a])/5-35),qiangdu[a])
wb.save(outfile)
else:
sheet.write((18-(Elevation[a]/5)),((Azimuth[a])/5+38),qiangdu[a])
wb.save(outfile)
#print qiangdu[a]