python 积累6(wind数据库对指数成分股的提取)

import cx_Oracle
import pandas as pd
import numpy as np
import os
os.chdir('C:\\Users\\jiangbaiyan\\Desktop\\找到行业的成分股')
os.getcwd()  


conn = cx_Oracle.connect('windquery/[email protected]:1521/winddb')
cur = conn.cursor() 
def search_securityID(windID):
    sql = ''' select F16_0001 from TB_OBJECT_0001 where F1_0001 = '%s' ''' % windID
    cur.execute(sql)
    temp = cur.fetchall()
    return temp[0][0]

def search_name(securityID):
    sql = ''' select F1_0001, F6_0001 from TB_OBJECT_0001 where F16_0001 = '%s' ''' % securityID
    cur.execute(sql)
    temp = cur.fetchall()
    return [temp[0][0], temp[0][1]]

def search_name2securityID(name):
    sql = ''' select F16_0001 from TB_OBJECT_0001 where F6_0001 = '%s' ''' % name
    cur.execute(sql)
    temp = cur.fetchall()
    return temp[0][0]

def get_stock(indexID):
    sql = ''' select F1_1475  from TB_OBJECT_1475 where F2_1475 = '%s' and F6_1475 = 1 ''' % (indexID)
    cur.execute(sql)
    temp = cur.fetchall()    
    return temp

def search_windID(securityID):
    sql = ''' select F1_0001 from TB_OBJECT_0001 where F16_0001 = '%s' ''' % securityID
    cur.execute(sql)
    temp = cur.fetchall()
    return temp[0][0]

industry = pd.read_excel('非重复行业.xlsx')
stock_wind = pd.read_excel('个股代码.xlsx')
industry.columns = ['方便阅读','简称']
industry_list = []
stock_list = []

for key in list(industry['简称']):
    print(key)
    industry_list.append(search_name2securityID(key))    
for key in list(stock_wind['代码']):
    print(key)
    stock_list.append(search_securityID(key))

dic = {}
for key in industry_list:
    temp =[]
    temp = get_stock(key)
    pd_temp = pd.DataFrame()
    for i,stock in enumerate(temp):
        pd_temp.loc[i,'股票ID'] = search_name(stock[0])[0]
        pd_temp.loc[i,'证券简称']= search_name(stock[0])[1]
        if stock[0] in stock_list:
            pd_temp.loc[i,'辅助列'] = 1
        else:
            pd_temp.loc[i,'辅助列'] = 0
        print(stock[0])    
    dic[search_name(key)[1]] = pd_temp
    print(key)
np.save('先进制造行业成分股.npy',dic)    


writer = pd.ExcelWriter('先进制造行业成分股.xlsx')
for key in dic:
    temp = dic[key]
    temp.to_excel(writer,sheet_name = key,index = 0)
    print(key)
writer.save()

你可能感兴趣的:(python 积累6(wind数据库对指数成分股的提取))