国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

[python]數據整理,將取得的眾多的滬深龍虎榜數據整一整

2019-11-14 16:58:55
字體:
來源:轉載
供稿:網友

將昨日取得的眾多的滬深龍虎榜數據整一整

提取文件夾內所有抓取下來的滬深龍虎榜數據,整理出滬深兩市(含中小創)漲幅榜股票及前5大買入賣出資金凈值,保存到csv文件

再手動使用數據透視表進行統計

原始數據:

整理后數據:

代碼如下(如果覺得對于炒股又用,敬請使用):

  1 #coding=utf-8  2   3 import re  4 import os  5 import time  6 import datetime  7   8 def writeFile(file,stocks,BS,day):  9     for s in stocks: 10         allfile.write('/n') 11         allfile.write(day 12                       +',"/''+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(day 23                       +",'"+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 in files: 45     if(os.path.isfile(path+'/'+f) & 46        f.endswith('.txt')): 47         # 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 = 0 68             readBuy = 0 69             readSell = 0 70             nowStock = '' 71             stocks = [] 72             BS = dict() 73             buy = 0 74             sell = 0 75             for info in infos: 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 = 0 96                     readBS = 1 97                     #continue 98  99                 if(readBS==1 and100                    info.startswith('_證券代碼')):101                     tmp = info.split('_')102                     #print('code:'+tmp[2])103                     nowStock = tmp[2]104                     readBS = 0105                     readBuy = 1106                     continue107                 108                 if(readBuy == 1 and109                    info.startswith('_(') and110                    (not info.startswith('_賣出'))):111                     tmp = info.split('_')112                     buy = buy + float(tmp[3])113                     #print('buy:'+str(buy))114                 elif(readBuy == 1 and115                    info.startswith('_賣出')):116                     readBuy = 0117                     readSell = 1118                     continue119                 120                 if(readSell == 1 and121                    info.startswith('_(') and122                    ((not info.startswith('_2')) or123                    (not info.startswith('_證券')))):124                     tmp = info.split('_')125                     sell = sell + float(tmp[3])126                     #print('sell:'+str(sell))127                 elif(readSell == 1 and128                    (info.startswith('_2') or129                    info.startswith('_證券'))):130                     readSell = 0131                     if(info.startswith('_證券')):132                         readBS = 1133                         #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 and139                            info.startswith('_證券代碼')):140                             tmp = info.split('_')141                             #print('code:'+tmp[2])142                             nowStock = tmp[2]143                             readBS = 0144                             readBuy = 1145                             continue146                         147                     else:148                         #dictTmp = {nowStock:{'buy':str(buy),'sell':str(sell)}}149                         BS[nowStock]={'buy':str(buy),'sell':str(sell)};150                         #write to doc151                         #print(stocks[0]['成交金額(萬元)'])152                         #print(BS)153                         154                         writeFile(allfile,stocks,BS,nowDayStr);155                         break;156                     157         else:158             #深證,中小創159             160             readStocks = 0161             #readBS = 0162             readBuy = 0163             readSell = 0164             nowStock = ''165             stocks = []166             BS = dict()167             buy = 0168             sell = 0169             threeBlank = 0170             for info in infos:171                 172                     173                 if(info.startswith('--') and readStocks==1 and len(stocks)>1):174                     readStocks=1175                     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 + 1195                     continue196                 if((not info.startswith('日漲幅偏離值達到7%的前五只證券')) and197                    readStocks==0 and readBuy==0 and readSell==0):198                     continue199                 elif(readStocks==0 and readBuy==0 and readSell==0):200                     201                     if(info.endswith('')):202                         203                         break204                     readStocks=1205                     continue206                 207                 if(#haveBreaked and208                    readStocks==1 and209                    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 = 1228                     continue229 230                 if(readBuy == 1 and info!='' and231                    (not info.startswith('買入金額最大的前5名')) and232                    (not info.startswith('營業部或交易單元名稱')) ):233                     #print('1'+info)234                     if(info.startswith('賣出金額最大的前5名')):235                         readBuy=0236                         readSell=1237                         continue238                     else:239                         buy = buy + float(info.split('_')[1]) - float(info.split('_')[2])240                         continue241 242                 if(readSell == 1 and info!='' and243                    (not info.startswith('營業部或交易單元名稱')) ):244                     #print('2'+info)245                     246                     if(info.startswith('--')):247                         readStocks=1248                         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=1263                         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 code273                         #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 = 1285                         continue286                         287                     else:288                         sell = sell - float(info.split('_')[1]) + float(info.split('_')[2])289                         continue290                 291         #break292 293 294 allfile.close();295 print('統計完成!'+'文件:'+'./滬深龍虎榜統計_'+nowStr+'.csv')

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 黑水县| 工布江达县| 克什克腾旗| 上蔡县| 陇南市| 乡城县| 南漳县| 乐陵市| 温泉县| 留坝县| 普格县| 柞水县| 岱山县| 简阳市| 甘南县| 禹州市| 喜德县| 漠河县| 东台市| 旅游| 当涂县| 淮阳县| 丽水市| 连州市| 化州市| 聊城市| 曲周县| 司法| 普宁市| 崇礼县| 会昌县| 井陉县| 甘德县| 蚌埠市| 宾阳县| 林芝县| 昆明市| 五华县| 安仁县| 济宁市| 西华县|