python龙虎榜数据_[python]数据整理,将取得的众多的沪深龙虎榜数据整一整

该代码段读取指定目录下多个文本文件,对上证和深证股票的龙虎榜数据进行处理,包括净流入流出、买入、卖出等信息的计算,并将结果写入CSV文件。程序首先按日期排序文件,然后逐个读取文件,通过正则表达式清洗数据,根据特定格式解析股票代码、名称、偏离值等关键指标。最后,将解析的数据写入到以当前日期命名的CSV文件中。
摘要由CSDN通过智能技术生成

1 #coding=utf-8

2

3 importre4 importos5 importtime6 importdatetime7

8 defwriteFile(file,stocks,BS,day):9 for s instocks:10 allfile.write('\n')11 allfile.write(day12 +',"\''+s['code']13 +'","'+s['name']14 +'",'+str(float(BS[s['code']]['buy'])-float(BS[s['code']]['sell']))15 +','+BS[s['code']]['buy']16 +','+BS[s['code']]['sell']17 +','+s['偏离值']18 +',"'+s['成交量']19 +'","'+s['成交金额(万元)']+'"')20

21 '''

22 allfile.write(day23 +",'"+s["code"]24 +"','"+s["name"]25 +"',"+str(float(BS[s["code"]]["buy"])-float(BS[s["code"]]["sell"]))26 +","+BS[s["code"]]["buy"]27 +","+BS[s["code"]]["sell"]28 +","+s["偏离值"]29 +",'"+s["成交量"]30 +"','"+s["成交金额(万元)"]+"'")31 '''

32

33 path=r'./files'

34 #path=r'./a'

35 files =os.listdir(path)36 files.sort()37

38 nowDayStr = ''

39 now =datetime.datetime.now()40 nowStr = now.strftime("%Y-%m-%d")41

42 allfile = open(r'./沪深龙虎榜统计_'+nowStr+'.csv','w')43 allfile.write('"日期","代码","名称","净流入流出","流入","流出","偏离值","成交量","成交金额(万元)"')44 for f infiles:45 if(os.path.isfile(path+'/'+f) &

46 f.endswith('.txt')):47 #print(path+'/'+f.replace('.txt',''))

48 a = f.replace('.txt','').split('_')49 print('读取文件:'+path+'/'+f)50 '''

51 if(nowDayStr!=a[0]):52 #print('a')53 else:54 #print('b')55 nowDayStr = a[0]56 '''

57 nowDayStr =a[0]58

59 f=open(path+'/'+f,'rt')60 infos =f.readlines()61 f.close()62

63 if(a[1]=='上证'):64 #continue #test jump

65 #上证

66 readStocks = 1

67 readBS =068 readBuy =069 readSell =070 nowStock = ''

71 stocks =[]72 BS =dict()73 buy =074 sell =075 for info ininfos:76

77 info = re.sub('\ +', '_',info)78 info = re.sub('\n', '',info)79

80 #print('line:' +info)

81 if(readStocks==1 and

82 info.startswith('_2')):83 break

84 if(readStocks==1 and

85 (not info.startswith('_证券代码:')) and

86 info.startswith('_(')):87

88 tmp = info.split('_')89 dictTmp = {'code':tmp[2],'name':tmp[3],'偏离值':tmp[4],'成交量':tmp[5],'成交金额(万元)':tmp[6]}90 stocks.append(dictTmp)91

92 elif(readStocks==1 and

93 info.startswith('_证券代码:')):94

95 readStocks =096 readBS = 1

97 #continue

98

99 if(readBS==1 and

100 info.startswith('_证券代码')):101 tmp = info.split('_')102 #print('code:'+tmp[2])

103 nowStock = tmp[2]104 readBS =0105 readBuy = 1

106 continue

107

108 if(readBuy == 1 and

109 info.startswith('_(') and

110 (not info.startswith('_卖出'))):111 tmp = info.split('_')112 buy = buy + float(tmp[3])113 #print('buy:'+str(buy))

114 elif(readBuy == 1 and

115 info.startswith('_卖出')):116 readBuy =0117 readSell = 1

118 continue

119

120 if(readSell == 1 and

121 info.startswith('_(') and

122 ((not info.startswith('_2')) or

123 (not info.startswith('_证券')))):124 tmp = info.split('_')125 sell = sell + float(tmp[3])126 #print('sell:'+str(sell))

127 elif(readSell == 1 and

128 (info.startswith('_2') or

129 info.startswith('_证券'))):130 readSell =0131 if(info.startswith('_证券')):132 readBS = 1

133 #dictTmp = {nowStock:{'buy':str(buy),'sell':str(sell)}}

134 BS[nowStock]={'buy':str(buy),'sell':str(sell)};135 buy =0136 sell =0137

138 if(readBS==1 and

139 info.startswith('_证券代码')):140 tmp = info.split('_')141 #print('code:'+tmp[2])

142 nowStock = tmp[2]143 readBS =0144 readBuy = 1

145 continue

146

147 else:148 #dictTmp = {nowStock:{'buy':str(buy),'sell':str(sell)}}

149 BS[nowStock]={'buy':str(buy),'sell':str(sell)};150 #write to doc

151 #print(stocks[0]['成交金额(万元)'])

152 #print(BS)

153

154 writeFile(allfile,stocks,BS,nowDayStr);155 break;156

157 else:158 #深证,中小创

159

160 readStocks =0161 #readBS = 0

162 readBuy =0163 readSell =0164 nowStock = ''

165 stocks =[]166 BS =dict()167 buy =0168 sell =0169 threeBlank =0170 for info ininfos:171

172

173 if(info.startswith('--') and readStocks==1 and len(stocks)>1):174 readStocks=1

175 readSell=0176 BS[nowStock]={'buy':str(buy),'sell':str(sell)};177 buy =0178 sell =0179 writeFile(allfile,stocks,BS,nowDayStr);180 break;181

182 #print('-----'+info)

183 if(threeBlank==3):184 threeBlank =0185 haveBreaked =True186 else:187 haveBreaked =False188

189 info = re.sub('\ +', '_',info)190 info = re.sub('\n', '',info)191

192 #print('line:' +info)

193 if(info == ''):194 threeBlank = threeBlank + 1

195 continue

196 if((not info.startswith('日涨幅偏离值达到7%的前五只证券')) and

197 readStocks==0 and readBuy==0 and readSell==0):198 continue

199 elif(readStocks==0 and readBuy==0 and readSell==0):200

201 if(info.endswith('无')):202

203 break

204 readStocks=1

205 continue

206

207 if(#haveBreaked and

208 readStocks==1 and

209 len(info.split('(代码'))>1):210

211 if(info.startswith('--')):212 #print(stocks)

213 #print(BS)

214 writeFile(allfile,stocks,BS,nowDayStr);215 break;216 #print('1'+info)

217 code = info.split('(代码')[1].split(')')[0]218 name = info.split('(代码')[0]219 plz = info.split('涨幅偏离值:')[1].split('_')[0]220 cjl = info.split('成交量:')[1].split('_')[0]221 cje = info.split('成交金额:_')[1]#.split('万元')[0]

222 nowStock =code223 dictTmp = {'code':code,'name':name,'偏离值':plz,'成交量':cjl,'成交金额(万元)':cje}224 stocks.append(dictTmp)225 #print(dictTmp)

226 readStocks =0227 readBuy = 1

228 continue

229

230 if(readBuy == 1 and info!='' and

231 (not info.startswith('买入金额最大的前5名')) and

232 (not info.startswith('营业部或交易单元名称')) ):233 #print('1'+info)

234 if(info.startswith('卖出金额最大的前5名')):235 readBuy=0236 readSell=1

237 continue

238 else:239 buy = buy + float(info.split('_')[1]) - float(info.split('_')[2])240 continue

241

242 if(readSell == 1 and info!='' and

243 (not info.startswith('营业部或交易单元名称')) ):244 #print('2'+info)

245

246 if(info.startswith('--')):247 readStocks=1

248 readSell=0249

250 #dictTmp = {nowStock:{'buy':str(buy),'sell':str(sell)}}

251 #print(nowStock)

252 BS[nowStock]={'buy':str(buy),'sell':str(sell)};253

254 buy =0255 sell =0256 #print(stocks)

257 #print(BS)

258 writeFile(allfile,stocks,BS,nowDayStr);259 break;260

261 if(len(info.split('代码'))>1):262 readStocks=1

263 readSell=0264

265 #dictTmp = {nowStock:{'buy':str(buy),'sell':str(sell)}}

266 #print(nowStock)

267 BS[nowStock]={'buy':str(buy),'sell':str(sell)};268

269 buy =0270 sell =0271

272 #read code

273 #print('2'+info)

274 code = info.split('(代码')[1].split(')')[0]275 name = info.split('(代码')[0]276 plz = info.split('涨幅偏离值:')[1].split('_')[0]277 cjl = info.split('成交量:')[1].split('_')[0]278 cje = info.split('成交金额:_')[1]#.split('万元')[0]

279 nowStock =code280 dictTmp = {'code':code,'name':name,'偏离值':plz,'成交量':cjl,'成交金额(万元)':cje}281 stocks.append(dictTmp)282 #print(dictTmp)

283 readStocks =0284 readBuy = 1

285 continue

286

287 else:288 sell = sell - float(info.split('_')[1]) + float(info.split('_')[2])289 continue

290

291 #break

292

293

294 allfile.close();295 print('统计完成!'+'文件:'+'./沪深龙虎榜统计_'+nowStr+'.csv')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值