python 一个处理数据的脚本

      最近在为公司写一个脚本,主要目的是抓取已有的一个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]


  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值