• 按照上下游图谱关系、增加上下游标签、生成json文件


    '''
    调整下游
    '''

    import csv
    import psycopg2
    import json
    class IO_rw(object):

    def __init__(self):

    self.conn = psycopg2.connect(database="postgres", user="postgres", password="123456", host="127.0.0.1", port="5432")
    self.cur = self.conn.cursor()

    self.read_down = []
    self.datadict_down = {}

    self.read_up = []
    self.datadict_up = {}

    self.file = open("you_num.json", "w", encoding="utf-8")
    #
    def process_item(self):

    self.cur.execute("select id,name,pid from bjzs_big_data.baoji_industry_level where pid = 0")
    rows = self.cur.fetchall()

    #拿到所有的一级分类
    dict = {}
    for row in rows:
    row = list(row)
    dict[row[0]] = row[1]
    li = list(dict.items())
    return li

    #下游
    def sql_dowm(self,i):

    self.cur.execute("select downid from bjzs_big_data.baoji_industry_chain where upid = {}".format(i))
    rows = self.cur.fetchall()
    downList = []
    for row in rows:
    downList.append(row[0])
    return downList

    #上游
    def sql_up(self,i):
    self.cur.execute("select upid from bjzs_big_data.baoji_industry_chain where downid = {}".format(i))
    rows = self.cur.fetchall()
    upList = []
    for row in rows:
    upList.append(row[0])
    return upList


    def getAllDowm(self, id, pid):
    temp = ()
    name = self.getNameByid(id)
    if pid is None:
    temp = (id, None, 'root', name)

    else:
    temp = (id, pid, 'down', name)
    # temp['topic'] = name
    # print(temp)
    #把当前的id放在 全局的read里面。用于防止重新查找

    #如果递归调用过、 这里就直接pass
    if temp in self.read_down:
    pass

    else:
    #把每一个temp放到read_down里面
    self.read_down.append(temp)

    ##sql查询查询下面的id
    downList = self.sql_dowm(id)

    # 递归调用
    for down in downList:
    self.getAllDowm(down, id)


    def getAllUp(self, id, pid):
    temp = ()
    name = self.getNameByid(id)
    if pid is None:
    temp = (id, None, 'root', name)

    else:
    temp = (id, pid, 'up', name)

    #把当前的id放在 全局的read里面。用于防止重新查找

    #如果递归调用过、 这里就直接pass
    if temp in self.read_up:
    pass

    else:
    #把每一个temp放到read_down里面
    self.read_up.append(temp)

    ##sql查询查询下面的id
    upList = self.sql_up(id)

    # 递归调用
    for down in upList:
    self.getAllUp(down, id)



    def getNameByid(self,id):
    self.cur.execute("select name from bjzs_big_data.baoji_industry_level where id = {}".format(id))
    rows = self.cur.fetchall()
    try:
    if rows == None:
    name = None
    else:
    name = rows[0][0]
    return name
    except:
    pass



    def write_json(self,data):
    #print(data)
    content = json.dumps(dict(data), ensure_ascii=False) + " "
    self.file.write(content)


    def get_key(self,data):
    dict_list = {}
    list_finally = []
    for t in data:
    dict_one = {}
    dict_one['id'] = t[0]
    dict_one['parentid'] = t[1]
    dict_one['type'] = t[2]
    dict_one['topic'] = t[3]

    # dict_one['id'] = t[0]
    # dict_one['parentid'] = t[1]
    # dict_one['topic'] = t[3]

    list_finally.append(dict_one)
    dict_list['data'] = list_finally
    return dict_list
    if __name__ == '__main__':

    r = IO_rw()
    li = r.process_item()

    print(li)

    for i in li:
    print('=====================', i[0])
    r.getAllDowm(i[0], None)

    #print(r.read_down) # 三个参数分别这样显示、 1、代表行业。 2、代表他是谁的上下游、 3、上游还是下游、 4、他的名字
    list_down = r.get_key(r.read_down)
    print("下游有:",list_down)

    r.getAllUp(i[0], None)

    #print(r.read_up)
    list_up = r.get_key(r.read_up)
    print(" 上游有:", list_up)

    r.write_json(list_down)
    r.write_json(list_up)
    r.read_down = []
    r.read_up = []

    r.cur.close()
    r.conn.close()
    r.file.close()
  • 相关阅读:
    NPM
    Angular2.0快速开始
    AngularJS常用插件与指令收集
    Linq 操作基础
    SQL Server2008 with(lock)用法
    SQL Server2008 MERGE指令用法
    SQL Server2008 表旋转(pivot)技术
    ef to sqlite 实际开发问题终极解决方法
    windows控件常用缩写
    SQL 查询总结
  • 原文地址:https://www.cnblogs.com/yuanjia8888/p/10177639.html
Copyright © 2020-2023  润新知