to tushare 导入股票数据

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/china_demon/article/details/52136452

import_oracle.py

import cx_Oracle  
import os
dns_tns=cx_Oracle.makedsn("192.168.1.10",1521,"orcl")  
print dns_tns  
con = cx_Oracle.connect('stock', 'stock', dns_tns)  
cur=con.cursor()


for path,d,filelist in os.walk('/root/stock/python/data/data/'):
    for filename in filelist:
        print filename 
        path = "/root/stock/python/data/data/" + filename 
        stockname = filename[0:8]
        for index,line in enumerate(file(path)):  
         sql="""insert into imp_python_stock 
          (file1,file2,file3,file4,file5,file6,file7,file8,file9,file10,file11,file12,file13,file14,file15,file16) values ('""" + stockname + """',"""
         for fields in (line.split(",")):  
          sql=sql+"'"+fields+"',"
         print sql
         cur.execute(sql[:-1]+")")  
         con.commit()  
con.close()

import_stock.py

#!/usr/bin/python

import tushare as ts
import os
import re
stocklistpath = '/root/stock/python/data/list/stocklist.txt'
savepath='/root/stock/python/data/data/'
openstock = open(stocklistpath,'r+')
for i in openstock:
  r = r"S[ZH]\d\d\d\d\d\d"
  stocklist = re.findall(r,i)
  stocknum =i[2:8]
  stockname =i[0:8]
  df = ts.get_hist_data(stocknum)
  print savepath
  print i
  df.to_csv(savepath + stockname  + '.txt')
  print stocknum
  print savepath

没有更多推荐了,返回首页