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

提取文件夹内所有抓取下来的沪深龙虎榜数据,整理出沪深两市(含中小创)涨幅榜股票及前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. eclipse build workspace太慢或者 js出错问题解决

    1.js文件错误解决办法 右键项目->properties->Builders(注:JavaScript Validator也会引起 build workspace太慢) 2.Eclips ...

  2. Ruby之入门(一)

    前言 这门语言很少去听过,可能是没怎么用到就不会听到太多关于ruby的消息,工作需要这门语言,需要从0开始学习这门语言,慢慢学习简直...太神奇了...,原谅我见识浅薄.原来很早就已经出世了,园子中也 ...

  3. RequireJs调研

    背景 Problem(问题) Web sites are turning into Web apps(网站正转变为网络应用程序) Code complexity grows as the site g ...

  4. 最大值最小化(DP)

    题目来源:网易有道2013年校园招聘面试一面试题 题目描述: 在印刷术发明之前,复制一本书是一个很困难的工作,工作量很大,而且需要大家的积极配合来抄写一本书,团队合作能力很重要.当时都是通过招募抄写员 ...

  5. NAS与SAN

    1. NAS(Network Attached Storage,网络附加存储服务器) 简单地说,NAS就是一台File Server,只要将NAS连接上网络,那么在网络上面的其他主机就能够访问NAS上 ...

  6. 【记录】T-SQL 分组排序中取出最新数据

    示例 Product 表结构: 示例 Product 表数据: 想要的效果是,以 GroupName 字段分组,取出分组中通过 Sort 降序最新的数据,通过示例数据,可以推算出结果数据的 ID 应该 ...

  7. Java 理论与实践: 正确使用 Volatile 变量--转

    原文地址:http://www.ibm.com/developerworks/cn/java/j-jtp06197.html Java 语言中的 volatile 变量可以被看作是一种 “程度较轻的  ...

  8. PJAX实现

    一.浏览器历史API 浏览器历史就像一堆卡片,如下所示:

  9. java.util.ConcurrentModificationException异常处理

    ConcurrentModificationException异常处理 ConcurrentModificationException异常是Iterator遍历ArrayList或者HashMap数组 ...

  10. jackson error 含义log

    1. 反序列化失败,类型不匹配 Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not deserial ize ...