#!/usr/bin/python #coding=utf-8 #--------------------------------------------- # 抓劵商相關資料 # Version : 1.1 # Author : Amin white # Release Date : 2012-01-01 # Python version : 2.7.2 #--------------------------------------------- import codecs, csv from lib import Broker, Broker2 def CheckBrokerAttribute(bankid): Brokerlist = ["9300"] #華南永昌 for i in range(0, len(Brokerlist)): if bankid[0] == Brokerlist[i][0] and bankid[1] == Brokerlist[i][1]: return u'公銀' Brokerlist = ["1020", #合庫 "1030", #土銀 "1040", #台銀 "1110", #台企 "1230", #彰銀 "5380", #第一金 "7000"] #兆豐 for i in range(0, len(Brokerlist)): if bankid[0] == Brokerlist[i][0] and bankid[1] == Brokerlist[i][1] and bankid[2] == Brokerlist[i][2]: return u'公銀' Brokerlist = [ "1360", #港商麥格理 "1380", #港商里昂 "1400", #港商蘇皇 "1440", #美林 "1470", #台灣摩根士丹利 "1480", #美商高盛 "1520", #瑞士信貸 "1530", #港商德意志 "1560", #港商野村台北分公司 "1570", #港商法國興業 "1590", #花旗環球 "1650", #新加坡商瑞銀 "6640", #渣打商銀 "8440", #摩根大通 "8700", #花旗 "8890", #大和國泰 "8900", #法銀巴黎 "8910", #台灣巴克萊 "8960"] #香港上海匯豐 for i in range(0, len(Brokerlist)): if bankid[0] == Brokerlist[i][0] and bankid[1] == Brokerlist[i][1] and bankid[2] == Brokerlist[i][2]: return u'外資' return '' def CheckMembership(MasterBrokerid, bankid): Brokerlist = [] Brokerlist = list(MasterBrokerid) removelist = ['9100', #群益 '9200', #凱基 '9300', #華南永昌 '9600', #富邦 '2200', #元大寶來 '9800'] #元大 #刪除特殊分類 for i in range(0, len(removelist)): Brokerlist.remove(removelist[i]) #比對所隸屬的總公司 for i in range(0, len(Brokerlist)): if bankid[0] == Brokerlist[i][0] and bankid[1] == Brokerlist[i][1] and bankid[2] == Brokerlist[i][2]: return Brokerlist[i] #比對特殊分類所隸屬的總公司 Brokerlist = [] Brokerlist = list(removelist) for i in range(0, len(Brokerlist)): if bankid[0] == Brokerlist[i][0] and bankid[1] == Brokerlist[i][1]: return Brokerlist[i] print "Unknow broker id : " + Brokerlist[i] return '' def Brokernamelist(): Brokerlist = Broker() Brokerfile = 'D:\\stock_database\\Brokerfile.csv' writefile = file(Brokerfile, 'wb') writefile.write(codecs.BOM_UTF8) writer = csv.writer(writefile, delimiter=',', quotechar='"', quoting=csv.QUOTE_ALL) writer.writerow([u'劵商代碼'.encode('utf8'), u'劵商名稱'.encode('utf8'), u'劵商他名'.encode('utf8'), \ u'劵商主從'.encode('utf8'), u'劵商屬性'.encode('utf8'), u'劵商隸屬'.encode('utf8'), \ u'劵商地址'.encode('utf8'), u'劵商電話'.encode('utf8'), u'劵商開業日期'.encode('utf8'),\ u'備註'.encode('utf8')]) for i in range(0, len(Brokerlist.brokerid)): brokerid = Brokerlist.brokerid[i] if brokerid == '6010': brokername = unicode('犇亞', "utf-8") else: brokername = unicode(Brokerlist.brokername[i], "big5") brokername = brokername.encode('utf8') brokeraddress = unicode(Brokerlist.brokeraddress[i], "big5") brokertel = unicode(Brokerlist.brokertel[i], "utf-8") brokeropen = unicode(Brokerlist.brokeropen[i], "utf-8") brokerProperty = CheckBrokerAttribute(brokerid) Membership = CheckMembership(Brokerlist.brokerid, brokerid) writer.writerow([ '%s' %brokerid.encode('utf8'), '%s' %brokername, '', \ '%s' %u'總公司'.encode('utf8'), '%s' %brokerProperty.encode('utf8'), '%s' %Membership.encode('utf8'), \ '%s' %brokeraddress.encode('utf8'), '%s' %brokertel.encode('utf8'), '%s' %brokeropen.encode('utf8'), '']) print brokerid + " " + brokername + " " + brokerProperty.encode('utf8') + " " + "write to csv OK......" Brokerlist2 = Broker2() for i in range(0,len(Brokerlist2.brokerid)): brokerid = Brokerlist2.brokerid[i] if brokerid == '6012': brokername = unicode('犇亞-網路', "utf-8").encode('utf8') else: brokername = unicode(Brokerlist2.brokername[i], "big5").encode('utf8') brokeraddress = unicode(Brokerlist2.brokeraddress[i], "big5") brokertel = unicode(Brokerlist2.brokertel[i], "big5") brokeropen = unicode(Brokerlist2.brokeropen[i], "utf-8") brokerProperty = CheckBrokerAttribute(brokerid) Membership = CheckMembership(Brokerlist.brokerid, brokerid) writer.writerow([ '%s' %brokerid.encode('utf8'), '%s' %brokername, '', \ '%s' %u'分公司'.encode('utf8'), '%s' %brokerProperty.encode('utf8'), '%s' %Membership.encode('utf8'), \ '%s' %brokeraddress.encode('utf8'), '%s' %brokertel.encode('utf8'), '%s' %brokeropen.encode('utf8'), '']) print brokerid + " " + brokername + " " + Membership + " " + "write to database OK......" writefile.close() def main(): Brokernamelist() if __name__ == "__main__": main()
Python執行畫面如下圖所示
CSV檔案儲存內容如下