• 标准国民经济行业分类与代码GB/T 4754-2011存入mysql数据库


    国标:

    代码:

     1 import pandas as pd
     2 import pymysql
     3 """
     4 ------------------------------------------------------------------------------------
     5 """
     6 def get_conn():
     7     """
     8     :return: 连接,游标
     9     """
    10     # 创建连接
    11     conn = pymysql.connect(host="127.0.0.1",
    12                     user="root",
    13                     password="000429",
    14                     db="data_cleaning",
    15                     charset="utf8")
    16     # 创建游标
    17     cursor = conn.cursor()  # 执行完毕返回的结果集默认以元组显示
    18     return conn, cursor
    19 
    20 def close_conn(conn, cursor):
    21     if cursor:
    22         cursor.close()
    23     if conn:
    24         conn.close()
    25 """
    26 -----------------------------------------------------------
    27 """
    28 """
    29 ------------------------------------------------------------------------------------
    30 """
    31 def query(sql,*args):
    32     """
    33     通用封装查询
    34     :param sql:
    35     :param args:
    36     :return:返回查询结果 ((),())
    37     """
    38     conn , cursor= get_conn()
    39     print(sql)
    40     cursor.execute(sql)
    41     res = cursor.fetchall()
    42     close_conn(conn , cursor)
    43     return res
    44 """
    45 ------------------------------------------------------------------------------------
    46 """
    47 
    48 def into_mysql(filename):
    49     category_code = ""      #门类编码
    50     category_name = ""      #门类名称
    51 
    52     conn,cursor=get_conn()  #连接mysql
    53     if(conn!=None):
    54         print("数据库连接成功!")
    55     tempres = []            #暂存列表
    56     df=pd.read_excel(filename)      #读取标准表
    57     # print(len(df.index))
    58     for i in range(len(df.index.values)):   #第一层遍历标准表 找到门类的编码和名称 找到小类的编码
    59         # print(df.loc[i][1])
    60         code=str(df.loc[i][0])           #所有的编码
    61         name=str(df.loc[i][1])           #所有的名称
    62         if len(code)==1:
    63             category_code=code     #门类编码
    64             category_name=name     #门类名称
    65         #分割编码
    66         if len(code)==4:
    67             small_class=name        #小类名称
    68             new_code_2=code[:2]     #分割出两位编码    之后确定大类名称
    69             new_code_3=code[:3]     #分割出三位编码    之后确定中类名称
    70             print(category_code)    #最终的字符串需要门类的编码ABCD和门类的名称
    71             print(new_code_2)
    72             print(new_code_3)
    73             for j in range(len(df.index.values)):   #第二次遍历 寻找不同的位数的编码对应不同的名称
    74                 if new_code_2==df.loc[j][0]:
    75                     big_class=df.loc[j][1]    #大类名称
    76                 if new_code_3==df.loc[j][0]:
    77                     mid_class=df.loc[j][1]    #中类名称
    78             tempres.append(category_code+code)              #列表暂存A0511 编码
    79             tempres.append(category_name+"·"+big_class+"·"+mid_class+"·"+small_class)   #列表暂存完整的名称
    80             print(tempres)
    81             SQL = "insert into std_code (code,name) values('"+tempres[0]+"','"+tempres[1]+"');"     #sql插入语句
    82             try:
    83                 cursor.execute(SQL)             #执行sql语句
    84                 conn.commit()                   #提交事务
    85                 print(""+str(i+1)+"条数据插入成功:
    ",category_code+code,name)        #插入成功输出
    86                 print("--------------------------------------------------")
    87             except:
    88                 print("插入失败:
    ",category_code+code,name)
    89             tempres=[]          #清空列表
    90     close_conn(conn,cursor)     #关闭数据库连接
    91     return None
    92 if __name__ == '__main__':
    93     filename="GBT4754-2011.xlsx"
    94     into_mysql(filename)

    运行代码输出的内容截图:

    最终存入mysql数据库截图:

  • 相关阅读:
    Java乔晓松oracle的多表查询
    定义一个地址固定的变量
    J2ME的表单元素及其事件
    J2me的异常处理和多线程
    [置顶] eclipse远程部署,静态文件实时同步插件
    newlisp debugger
    启用isqlplus
    ORA27101: shared memory realm does not exist
    windows下安装node.js及less
    em启动/关闭报错解决
  • 原文地址:https://www.cnblogs.com/rainbow-1/p/15468149.html
Copyright © 2020-2023  润新知