首页 > 数据库 >python stata转mysql

python stata转mysql

时间:2023-03-24 17:35:48浏览次数:40  
标签:ab mc python ii connect stata mysql print con

import numpy as np
import pyreadstat as pyreadstat
import json,re,random,pymysql,configparser,sys
import pandas as pd
from duconfig import read_ini
def dujiegou(mc,ab):
    dataframe, meta = pyreadstat.read_dta(ab)
    bla=meta.original_variable_types
    ch="create table "+mc+"("
    for ii in bla:
        lxa=""
        cd=""
        #print(ii,bla[ii])
        mc=ii
        lx=bla[ii].replace('%-','').replace('%','')
        if lx.endswith('s'):
            lxa="varchar"
            cd=lx.replace('s','')
        elif lx.endswith('gc') or lx.endswith('f') or lx.endswith('g') or lx.endswith('d'):
            lxa="float"
            #cd=lx.replace('gc','').replace('f','').replace('g','')
            #cd=cd.split('.')
            #cd=cd[0]
        if mc=="foreign":
            mc="forei"
        #ch=ch+mc+" "+lxa+"("+cd+"),"
        if cd!="":
            ch=ch+mc+" "+lxa+"("+cd+"),"
        else:
            ch=ch+mc+" "+lxa+","
    return ch[:-1]+")"

def dumc(mca,ab):
    dataframe, meta = pyreadstat.read_dta(ab)
    mc=meta.column_names
    ab="insert ignore into "+mca+" ("
    abb=""
    for ii in mc:
        #ab=ab+"'"+ii+"',"
        if ii=="foreign":
            ii="forei"
        ab=ab+ii+","
        abb=abb+"%s,"
    return ab[:-1]+") values ("+abb[:-1]+")"
def dumca(ab):
    dataframe, meta = pyreadstat.read_dta(ab)
    mc=meta.column_names
    return mc

    
def charu(sqlx,uuk):
    con = connect.cursor()
    try:
        con.execute(sqlx,uuk)
        connect.commit()
        print("保存数据成功")
    except Exception as e:
        connect.rollback()

def charua(uuk):
    con = connect.cursor()
    try:
        con.executemany('insert ignore into f4 values (%s,%s)',uuk)
        connect.commit()
        print("保存数据成功")
    except Exception as e:
        connect.rollback()
        
def get_all(sql, params=()):
    global con,connect
    result = None
    try:
        con.execute(sql, params)
        result = con.fetchall()
        #.close()
    except Exception as e:
        print(e)
    return result
def zxupdate(uuk):
    global con,connect
    con = connect.cursor()
    try:
        con.execute(uuk)
        connect.commit()
        print("更新数据成功")
    except Exception as e:
        connect.rollback()
user= read_ini("section0",'user')
passwd=read_ini("section0",'user1')
dbname=read_ini("section0",'user2')
ip=read_ini("section0",'user3')
connect = pymysql.connect(user =user,
            password =passwd,
            db =dbname,
            host =ip,
            port = 3306,
            charset = 'utf8'
            )
con = connect.cursor()        
a="d:\\statashu\\tv3.dta"
sql1=dujiegou("tvv",a)
sql2=dumc("tvv",a)
print(sql1)
zxupdate(sql1)
print(sql2)

blm=dumca(a)
print(blm)
df = pd.read_stata(a)
for index, row in df.iterrows():
    lba=[]
    for ii in blm:
        print(f"Index: {index}, Row: {row[ii]}")
        iux=np.nan_to_num(row[ii],nan=99999)#判断如果是缺失值,则改为99999
        lba.append(iux)
    print(lba)
    charu(sql2,lba)
    #break
    
sys.exit(0)

 

标签:ab,mc,python,ii,connect,stata,mysql,print,con
From: https://www.cnblogs.com/xkdn/p/17252813.html

相关文章