Python解析Json⽂件,写⼊到mysql
import json
import pymysql
def prem(db):
cursor = db.cursor()
data = cursor.fetchone()
print("Database version : %s "% data)# 结果表明已经连接成功
sql ="""CREATE TABLE review (
thread是什么意思英语翻译** VARCHAR(100)"""
def reviewdata_insert(db,dir_name,tablename,tup):
with open(dir_name,'r', encoding='utf-8')as f:
i =0
while True:零基础自学编程应该看什么书
i +=1
print(u'正在载⼊第%s⾏......'% i)
#try:
lines = f.readline()
php跳转网址代码if len(lines)>0:
review_text = json.loads(lines)
python请求并解析json数据
else:
break
result =[]
result.append(([review_text[i]for i in tup]))
print(result)
mid =""
per =""
for field in tup:
华为开发者直播mid += field+","
per +="%s,"
print(mid[:-1])
print(per[:-1])
inesrt_re ="insert into {0}".format(tablename)+"("+mid[:-1]+") values ("+ per[:-1]+")"
print(inesrt_re)
cursor = db.cursor()
dbmit()
# except Exception as e:
# db.rollback()
# print(str(e))
# break
if __name__ =="__main__":# 起到⼀个初始化或者调⽤函数的作⽤
db = t("localhost","root","root","sobot_db", charset='utf8')
z型折弯模图纸cursor = db.cursor()
#prem(db)
tablename ="rpt_stafflogin_log"
json_name ='test.json'
tup =['logType','companyId','datetime','loginLogType','source','operation','staffId']
reviewdata_insert(db,json_name,tablename,tup)
cursor.close()
测试数据
{“logType”:2,“companyId”:“51198d62b6574403a0b7e85d72d31b78”,“datetime”:1557804551790,“loginLogTy pe”:0,“source”:0,“operation”:2,“staffId”:“1ebcc059b3ca478aaecde4b5e9fca9df”}
{“logType”:2,“companyId”:“51198d62b6574403a0b7e85d72d31b78”,“datetime”:1557806744163,“loginLogTy pe”:0,“source”:0,“operation”:0,“staffId”:“efef7570fa9b4021890d815144776a99”}
{“logType”:2,“companyId”:“51198d62b6574403a0b7e85d72d31b78”,“datetime”:1558767573813,“loginLogTy pe”:0,“source”:0,“operation”:6,“staffId”:“73177bcddbf047f596bc9a6e6574abc4”}
{“logType”:2,“companyId”:“51198d62b6574403a0b7e85d72d31b78”,“datetime”:1559361115531,“loginLogTy pe”:0,“source”:0,“operation”:0,“staffId”:“c107d24efccc4096ae0a37eba093bf9a”}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论