# -*- coding:utf-8 -*-
import json
import pymysql
for m in range(1,225):
file = 'F:\\pins_two\\user_xinxi\\pins_two_'+str(m)+'.json' //循环目录下的文件
fp = open(file, 'r')
str1 = fp.read()
ls = eval(str1)
connent = pymysql.connect(host='localhost', user='root', passwd='123', db='users', charset='utf8')
cursor = connent.cursor()
usersvalues=[] //定义一个列表
for i in range(0,len(ls)) :
values_list=list(ls[i].values())
values_list1=list(values_list[0].values()) //将字典的的值转为列表
values_list2=values_list1+values_list[1:] //列表的拼接
usersvalues.append(values_list2)
sql="insert into user_xinxi(url_name,web_site,twitter,following,follower,facebook,board,full_name,pins,_id,user_id) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
try:
cursor.executemany(sql,usersvalues) //批量插入函数
connent.commit()
except Exception as e:
print("Error",str(e),sql)
connent.close()
网友评论