手动下载股票列表 存入mongodb 并更新名字

#! /usr/bin/python2
# coding=utf-8
import os
import csv
import pymongo


global stocks

'''
mongodb 删除数据库
use test;
db.dropDatabase();
mongodb删除表
db.mytable.drop();
清空表
db.mytable.remove({})
'''


class c_stock:
    def __init__(self, code, name):
        self.code = code
        self.name = name

def init_mongodb():
    global stocks
    # mongodb_link = 'mongodb://127.0.0.1:27017'
    # mongoClient = MongoClient(mongodb_link)
    conn = pymongo.MongoClient("localhost", 27017)
    conn.stock_list.authenticate("d", "zz")
    db = conn.stock_list
    stocks = db.stocks
    #return stocks


def write_dict(type):
    stocks.save(type.__dict__)
def deal_data(code1, name1, date_start1, share_total1, share_outstanding1):
    code = code1.strip()
    name = name1.strip()
    date_start = date_start1.strip()
    share_total = share_total1.strip()
    share_outstanding = share_outstanding1.strip()
    cnt = stocks.find({"code":code}).count()
    #print cnt,"cnt"
    if (1 == cnt):
        l = stocks.find_one({"code": code})
        #for i in l:
         #   print i.get("name")
        #    print i.get("code")
        name_find = l.get("name")
        #name = "adc3"
        #str.decode("g18030") == str.decode("utf8")
        #if (0 != cmp(name, name_find)):
        if (name.decode("utf8") != name_find.decode("utf8")):
            stocks.update({"code":code}, {"$set":{"name":name, "date_start":date_start, "share_total":share_total ,"share_outstanding":share_outstanding}})
            print "------update name-------", name, name_find
            #stocks.update({"code": "123456"}, {$set:{"name": "babc123"}})
    elif (0 == cnt):
        print "------insert name-------"
        stocks.insert({"code": code, "name": name, "date_start":date_start, "share_total":share_total ,"share_outstanding":share_outstanding})
    else:
        print "------remove name-------", cnt
        stocks.remove({"code": code})
        stocks.insert({"code": code, "name":name, "date_start":date_start, "share_total":share_total ,"share_outstanding":share_outstanding})



def get_stock_list_sh():
    list_name = ['sha.csv', 'shb.csv']
    for name in list_name:
        try:
            csvfile = file(name, 'rb')
            reader = csv.reader(csvfile)

            for line in reader:
                try:
                    # 忽略第一行
                    if reader.line_num == 1:
                        continue
                    #list_code.append(line[0])
                    # print(' said: ', '')
                    i = 2
                    print line[i], line[i+1], line[i+2], line[i+3], line[i+4]
                    deal_data(line[i], line[i+1], line[i+2], line[i+3], line[i+4])

                except ValueError:
                    pass
            print "name --",name, locals()
            # csvfile.close()
        except IOError as err:  # 使用as将异常对象,并将其赋值给一个标识符
            print('File Error:' + str(err))  # ‘+’用于字符串直接的连接

        finally:
            if 'csvfile' in locals():
                csvfile.close()
                print "close"


def get_stock_list_sz():
    list_name = ['sz.csv']
    for name in list_name:
        try:
            csvfile = file(name, 'rb')
            reader = csv.reader(csvfile)

            for line in reader:
                try:
                    # 忽略第一行
                    if reader.line_num == 1:
                        continue
                    #list_code.append(line[0])
                    # print(' said: ', '')
                    i = 5
                    j = 5
                    #print line
                    print "a---",line[i], line[i + 1], line[i + 2], line[i + 3], line[i + 4]
                    print "b---",line[i + j], line[i + 1+ j], line[i + 2+ j], line[i + 3+ j], line[i + 4+ j]
                    if ('' != line[i]):
                        deal_data(line[i], line[i + 1], line[i + 2], line[i + 3], line[i + 4])
                    #print line[i + j]
                    if ('' != line[i + j]):
                        print "-------------------sz---------b-----------------",line[i + j]
                        deal_data(line[i + j], line[i + 1+ j], line[i + 2+ j], line[i + 3+ j], line[i + 4+ j])

                except ValueError:
                    pass
            print "anme2 ---", name, locals()
            # csvfile.close()
        except IOError as err:  # 使用as将异常对象,并将其赋值给一个标识符
            print('File Error:' + str(err))  # ‘+’用于字符串直接的连接

        finally:
            if 'csvfile' in locals():
                csvfile.close()
                print "close"

if __name__ == '__main__':
    init_mongodb()
    print stocks.count()
    #stocks.drop();
    get_stock_list_sh()
    get_stock_list_sz()
    print stocks.count()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值