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

提取文件夹内所有抓取下来的沪深龙虎榜数据,整理出沪深两市(含中小创)涨幅榜股票及前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. 将数据从MySQL迁移到Oracle的注意事项

    将数据从MySQL迁移到Oracle的注意事项1.自动增长的数据类型处理MYSQL有自动增长的数据类型,插入记录时不用操作此字段,会自动获得数据值.ORACLE没有自动增长的数据类型,需要建立一个自动 ...

  2. 初学Python

    初学Python 1.Python初识 life is short you need python--龟叔名言 Python是一种简洁优美语法接近自然语言的一种全栈开发语言,由"龟叔&quo ...

  3. jquery $.each的用法

    通过它,你可以遍历对象.数组的属性值并进行处理. 使用说明 each函数根据参数的类型实现的效果不完全一致: 1.遍历对象(有附加参数) $.each(Object, function(p1, p2) ...

  4. jsp通过易宝方式实现在线支付

    项目下载地址: https://github.com/hjzgg/OnlinePayment 参考:http://blog.csdn.net/jadyer/article/details/738025 ...

  5. Util应用程序框架公共操作类(六):验证扩展

    前面介绍了仓储的基本操作,下面准备开始扩展查询,在扩展查询之前,首先要增加两个公共操作类,一个是经常要用到的验证方法,另一个是Lambda表达式的操作类. 很多时候,我们会判断一个对象是否为null, ...

  6. MySQL分区总结

    MySQL支持RANGE,LIST,HASH和KEY四种分区.其中,每个分区又都有一种特殊的类型.对于RANGE分区,有RANGE COLUMNS分区.对于LIST分区,有LIST COLUMNS分区 ...

  7. sqoop数据导出导入命令

    1. 将mysql中的数据导入到hive中 sqoop import --connect jdbc:mysql://localhost:3306/sqoop --direct --username r ...

  8. geotrellis使用(三)geotrellis数据处理过程分析

    之前简单介绍了geotrellis的工作过程以及一个简单的demo,最近在此demo的基础上实现了SRTM DEM数据的实时分析以及高程实时处理,下面我就以我实现的上述功能为例,简单介绍一下geotr ...

  9. 让自己也能使用Canvas

    <canvas> 是 HTML5 新增的元素,可使用JavaScript脚本来绘制图形.例如:画图,合成照片,创建动画甚至实时视频处理与渲染. 兼容性方面,除了一些骨灰级浏览器IE6.IE ...

  10. struts2学习笔记--使用servletAPI实现ajax的一个小Demo

    这个例子是点击网页上的一个button,然后调用action,使用response项前台打印"哎呦 不错哦",当然是以异步形式实现. jsp页面: <head> < ...