读取通达信本地数据,并保存在mongodb数据库

来源:互联网 发布:linux 虚机网络不通 编辑:程序博客网 时间:2024/06/05 01:12
# coding: UTF-8from struct import *import osimport sys    def day2csv_data(dirname,fname,targetDir):    ofile=open(dirname+os.sep+fname,'rb')    buf=ofile.read()    ofile.close()         ifile=open(targetDir+os.sep+fname[:-4]+'.csv','w')    num=len(buf)    no=int(num/32)    b=0    e=32    line=''     linename=str('date')+' ,'+str('open')+', '+str('high')+' ,'+str('low')+', '+str('close')+' ,'+str('amout')+', '+str('vol')+' ,'+str('str07')+''+'\n'      # print line    ifile.write(linename)    for i in range(no):       a=unpack('IIIIIfII',buf[b:e])       line=str(a[0])+' ,'+str(a[1]/100.0)+', '+str(a[2]/100.0)+', '+str(a[3]/100.0)+', '+str(a[4]/100.0)+' ,'+str(a[5]/10.0)+', '+str(a[6])+' ,'+str(a[7])+''+'\n'      # print line       ifile.write(line)       b=b+32       e=e+32    ifile.close()    pathdir='c:/new_tdx/vipdoc/sz/lday'targetDir='c:/new_tdx/vipdoc/sz/pythondata/'listfile=os.listdir(pathdir)listfile[2][2:-4] for f in listfile:       day2csv_data(pathdir,f,targetDir)else:    print ('The for '+pathdir+' to '+targetDir+'  loop is over')

import osimport sysimport pymongoimport pandas as pdfile_path='c:/new_tdx/vipdoc/sz/pythondata/'listfile=os.listdir(file_path)def save_df_mongodb(file_path):    client=pymongo.MongoClient()    listfile=os.listdir(file_path)    for f in listfile:        file_url=file_path+f        df=pd.read_csv(file_url)        conn=client.db[f[2:-4]]        conn.insert_many(df.to_dict(orient='records'))        print(f[2:-4])save_df_mongodb(file_path)#获取mongodb数据库数据代码name_list=[]file_path='c:/new_tdx/vipdoc/sh/pythondata/'listfile=os.listdir(file_path)for f in listfile:    name_list.append(f[2:-4])len(name_list)df=pd.DataFrame(name_list)df.columns=['stock_name']df.to_csv('mongodb_stock_name.csv')

阅读全文
0 0
原创粉丝点击