将昨日取得的众多的沪深龙虎榜数据整一整

提取文件夹内所有抓取下来的沪深龙虎榜数据,整理出沪深两市(含中小创)涨幅榜股票及前5大买入卖出资金净值,保存到csv文件

再手动使用数据透视表进行统计

原始数据:

整理后数据:

代码如下(如果觉得对于炒股又用,敬请使用):

 #coding=utf-8

 import re
import os
import time
import datetime def writeFile(file,stocks,BS,day):
for s in stocks:
allfile.write('\n')
allfile.write(day
+',"\''+s['code']
+'","'+s['name']
+'",'+str(float(BS[s['code']]['buy'])-float(BS[s['code']]['sell']))
+','+BS[s['code']]['buy']
+','+BS[s['code']]['sell']
+','+s['偏离值']
+',"'+s['成交量']
+'","'+s['成交金额(万元)']+'"') '''
allfile.write(day
+",'"+s["code"]
+"','"+s["name"]
+"',"+str(float(BS[s["code"]]["buy"])-float(BS[s["code"]]["sell"]))
+","+BS[s["code"]]["buy"]
+","+BS[s["code"]]["sell"]
+","+s["偏离值"]
+",'"+s["成交量"]
+"','"+s["成交金额(万元)"]+"'")
''' path=r'./files'
#path=r'./a'
files = os.listdir(path)
files.sort() nowDayStr = ''
now = datetime.datetime.now()
nowStr = now.strftime("%Y-%m-%d") allfile = open(r'./沪深龙虎榜统计_'+nowStr+'.csv','w')
allfile.write('"日期","代码","名称","净流入流出","流入","流出","偏离值","成交量","成交金额(万元)"')
for f in files:
if(os.path.isfile(path+'/'+f) &
f.endswith('.txt')):
#print(path+'/'+f.replace('.txt',''))
a = f.replace('.txt','').split('_')
print('读取文件:'+path+'/'+f)
'''
if(nowDayStr!=a[0]):
#print('a')
else:
#print('b')
nowDayStr = a[0]
'''
nowDayStr = a[0] f=open(path+'/'+f,'rt')
infos = f.readlines()
f.close() if(a[1]=='上证'):
#continue #test jump
#上证
readStocks = 1
readBS = 0
readBuy = 0
readSell = 0
nowStock = ''
stocks = []
BS = dict()
buy = 0
sell = 0
for info in infos: info = re.sub('\ +', '_',info)
info = re.sub('\n', '',info) #print('line:' +info)
if(readStocks==1 and
info.startswith('_2')):
break
if(readStocks==1 and
(not info.startswith('_证券代码:')) and
info.startswith('_(')): tmp = info.split('_')
dictTmp = {'code':tmp[2],'name':tmp[3],'偏离值':tmp[4],'成交量':tmp[5],'成交金额(万元)':tmp[6]}
stocks.append(dictTmp) elif(readStocks==1 and
info.startswith('_证券代码:')): readStocks = 0
readBS = 1
#continue if(readBS==1 and
info.startswith('_证券代码')):
tmp = info.split('_')
#print('code:'+tmp[2])
nowStock = tmp[2]
readBS = 0
readBuy = 1
continue if(readBuy == 1 and
info.startswith('_(') and
(not info.startswith('_卖出'))):
tmp = info.split('_')
buy = buy + float(tmp[3])
#print('buy:'+str(buy))
elif(readBuy == 1 and
info.startswith('_卖出')):
readBuy = 0
readSell = 1
continue if(readSell == 1 and
info.startswith('_(') and
((not info.startswith('_2')) or
(not info.startswith('_证券')))):
tmp = info.split('_')
sell = sell + float(tmp[3])
#print('sell:'+str(sell))
elif(readSell == 1 and
(info.startswith('_2') or
info.startswith('_证券'))):
readSell = 0
if(info.startswith('_证券')):
readBS = 1
#dictTmp = {nowStock:{'buy':str(buy),'sell':str(sell)}}
BS[nowStock]={'buy':str(buy),'sell':str(sell)};
buy = 0
sell = 0 if(readBS==1 and
info.startswith('_证券代码')):
tmp = info.split('_')
#print('code:'+tmp[2])
nowStock = tmp[2]
readBS = 0
readBuy = 1
continue else:
#dictTmp = {nowStock:{'buy':str(buy),'sell':str(sell)}}
BS[nowStock]={'buy':str(buy),'sell':str(sell)};
#write to doc
#print(stocks[0]['成交金额(万元)'])
#print(BS) writeFile(allfile,stocks,BS,nowDayStr);
break; else:
#深证,中小创 readStocks = 0
#readBS = 0
readBuy = 0
readSell = 0
nowStock = ''
stocks = []
BS = dict()
buy = 0
sell = 0
threeBlank = 0
for info in infos: if(info.startswith('--') and readStocks==1 and len(stocks)>1):
readStocks=1
readSell=0
BS[nowStock]={'buy':str(buy),'sell':str(sell)};
buy = 0
sell = 0
writeFile(allfile,stocks,BS,nowDayStr);
break; #print('-----'+info)
if(threeBlank==3):
threeBlank = 0
haveBreaked = True
else:
haveBreaked = False info = re.sub('\ +', '_',info)
info = re.sub('\n', '',info) #print('line:' +info)
if(info == ''):
threeBlank = threeBlank + 1
continue
if((not info.startswith('日涨幅偏离值达到7%的前五只证券')) and
readStocks==0 and readBuy==0 and readSell==0):
continue
elif(readStocks==0 and readBuy==0 and readSell==0): if(info.endswith('无')): break
readStocks=1
continue if(#haveBreaked and
readStocks==1 and
len(info.split('(代码'))>1): if(info.startswith('--')):
#print(stocks)
#print(BS)
writeFile(allfile,stocks,BS,nowDayStr);
break;
#print('1'+info)
code = info.split('(代码')[1].split(')')[0]
name = info.split('(代码')[0]
plz = info.split('涨幅偏离值:')[1].split('_')[0]
cjl = info.split('成交量:')[1].split('_')[0]
cje = info.split('成交金额:_')[1]#.split('万元')[0]
nowStock = code
dictTmp = {'code':code,'name':name,'偏离值':plz,'成交量':cjl,'成交金额(万元)':cje}
stocks.append(dictTmp)
#print(dictTmp)
readStocks = 0
readBuy = 1
continue if(readBuy == 1 and info!='' and
(not info.startswith('买入金额最大的前5名')) and
(not info.startswith('营业部或交易单元名称')) ):
#print('1'+info)
if(info.startswith('卖出金额最大的前5名')):
readBuy=0
readSell=1
continue
else:
buy = buy + float(info.split('_')[1]) - float(info.split('_')[2])
continue if(readSell == 1 and info!='' and
(not info.startswith('营业部或交易单元名称')) ):
#print('2'+info) if(info.startswith('--')):
readStocks=1
readSell=0 #dictTmp = {nowStock:{'buy':str(buy),'sell':str(sell)}}
#print(nowStock)
BS[nowStock]={'buy':str(buy),'sell':str(sell)}; buy = 0
sell = 0
#print(stocks)
#print(BS)
writeFile(allfile,stocks,BS,nowDayStr);
break; if(len(info.split('代码'))>1):
readStocks=1
readSell=0 #dictTmp = {nowStock:{'buy':str(buy),'sell':str(sell)}}
#print(nowStock)
BS[nowStock]={'buy':str(buy),'sell':str(sell)}; buy = 0
sell = 0 #read code
#print('2'+info)
code = info.split('(代码')[1].split(')')[0]
name = info.split('(代码')[0]
plz = info.split('涨幅偏离值:')[1].split('_')[0]
cjl = info.split('成交量:')[1].split('_')[0]
cje = info.split('成交金额:_')[1]#.split('万元')[0]
nowStock = code
dictTmp = {'code':code,'name':name,'偏离值':plz,'成交量':cjl,'成交金额(万元)':cje}
stocks.append(dictTmp)
#print(dictTmp)
readStocks = 0
readBuy = 1
continue else:
sell = sell - float(info.split('_')[1]) + float(info.split('_')[2])
continue #break allfile.close();
print('统计完成!'+'文件:'+'./沪深龙虎榜统计_'+nowStr+'.csv')

[python]数据整理,将取得的众多的沪深龙虎榜数据整一整的更多相关文章

  1. [python]沪深龙虎榜数据导入通达信的自选板块,并标注于K线图上

    将沪深龙虎榜数据导入通达信的自选板块,并标注于K线图上 原理:python读取前一次处理完的计算5日后涨跌幅输出的csv文件 文件名前加"[paint]" 安照通达信的画图文件和板 ...

  2. [python]沪深龙虎榜数据进一步处理,计算日后5日的涨跌幅

    沪深龙虎榜数据进一步处理,计算日后5日的涨跌幅 事前数据: 前面处理得到的csv文件 文件名前加入“[wait]”等待程序处理 python代码从雅虎股票历史数据api获取数据,计算后面5日的涨跌幅 ...

  3. Python模块整理(三):子进程模块subprocess

    文章 原始出处 http://ipseek.blog.51cto.com/1041109/807513. 本来收集整理网络上相关资料后整理: 从python2.4版本开始,可以用subprocess这 ...

  4. 基于python的《Hadoop权威指南》一书中气象数据下载和map reduce化数据处理及其可视化

    文档内容: 1:下载<hadoop权威指南>中的气象数据 2:对下载的气象数据归档整理并读取数据 3:对气象数据进行map reduce进行处理 关键词:<Hadoop权威指南> ...

  5. Pandas数据处理实战:福布斯全球上市企业排行榜数据整理

    手头现在有一份福布斯2016年全球上市企业2000强排行榜的数据,但原始数据并不规范,需要处理后才能进一步使用. 本文通过实例操作来介绍用pandas进行数据整理. 照例先说下我的运行环境,如下: w ...

  6. 孤荷凌寒自学python第六十天在windows10上搭建本地Mongodb数据服务

     孤荷凌寒自学python第六十天在windows10上找搭建本地Mongodb数据服务 (完整学习过程屏幕记录视频地址在文末) 今天是学习mongoDB数据库的第六天.成功在本地搭建了windows ...

  7. 【Python文件处理】递归批处理文件夹子目录内所有txt数据

    因为有个需求,需要处理文件夹内所有txt文件,将txt里面的数据筛选,重新存储. 虽然手工可以做,但想到了python一直主张的是自动化测试,就想试着写一个自动化处理数据的程序. 一.分析数据格式 需 ...

  8. [python]初试页面抓取——抓取沪深股市交易龙虎榜数据

    [python]抓取沪深股市交易龙虎榜数据 python 3.5.0下运行 没做自动建立files文件夹,需要手动在py文件目录下建立files文件夹后运行 #coding=utf-8 import ...

  9. 一些用于数据整理的excel函数

    我们经常要从外部数据源(如数据库.文本文件或网页等)将数据导入excel中,但是此类数据往往比较混乱,无法满足我们的要求,因此在进行数据分析之前,需要将这些数据进行整理清洗,excel由于将数据的管理 ...

随机推荐

  1. 理解javascript中的对话框

    前面的话 通常我们调试程序时,如果需要阻塞效果,则要用到alert().但除了alert()以外,window对象还提供了其他3种对话框.本文将详细介绍window对象中的对话框 定义 系统对话框与在 ...

  2. Android开发之Toast

    第一次在博客园发布文章,就把我刚弄明白的关于Android开发中的提示设置,分享给大家. Toast是Android中经常用到的一个方法,用于简单的用户提示,经过摸索我发现了Toast的两种使用方式, ...

  3. 【开源】OSharp3.0框架解说系列:新版本说明及新功能规划预览

    OSharp是什么? OSharp是个快速开发框架,但不是一个大而全的包罗万象的框架,严格的说,OSharp中什么都没有实现.与其他大而全的框架最大的不同点,就是OSharp只做抽象封装,不做实现.依 ...

  4. 【前端性能】必须要掌握的原生JS实现JQuery

    很多时候,我们经常听见有人说jquery有多快多快.在这个各种类库满天飞的时候,不得不说的是,能有原生JS快吗? 是的,明显原生JS要更快,因为诸如JQuery这样的库必须要兼容各种浏览器和低版本和许 ...

  5. 分析Mysql 5.6的Dockerfile

    Docker官方的Mysql镜像的Dockerfile托管在Github上,地址如下: https://github.com/docker-library/mysql/tree/5836bc9af9d ...

  6. EF7 Code First Only-所引发的一些“臆想”

    At TechEd North America we were excited to announce our plans for EF7, and even demo some very early ...

  7. Ios 实现 滑动cell 伸缩的核心代码

    - (void)scrollViewDidScroll:(UIScrollView *)scrollView { CGFloat viewHeight = scrollView.height + sc ...

  8. JavaWeb路径问题打包总结--小心出门右转404

    话说,培训和自学就不是一个回事,两周讲完java基础,两天讲完jsp,两节课讲完servlet,还真不是一般人能受得了的,这两天学习jsp和servlet频繁被路径问题困扰,倒不是出错,只是各种act ...

  9. jQuery-1.9.1源码分析系列(一)整体架构

    不废话,直接上关键.这个系列中有好些直接借用别人的资料,我将他们整合在自认为比较合理的地方.所以在此先谢谢那些前辈. 注意:后续系列中jQuery实例多用$(...)来表示 1.    初始化与链式调 ...

  10. 百度地图、ECharts整合HT for Web网络拓扑图应用

    前一篇谈及到了ECharts整合HT for Web的网络拓扑图应用,后来在ECharts的Demo中看到了有关空气质量的相关报表应用,就想将百度地图.ECharts和HT for Web三者结合起来 ...