码迷,mamicode.com
首页 > 数据库 > 详细

json数据生成mySQL语句

时间:2018-05-21 12:46:34      阅读:445      评论:0      收藏:0      [点我收藏+]

标签:def   name   int   key   category   ecs   SQ   product   tor   

import json,codecs

def readjson(file):
with open(file,‘rb‘) as fp:
data = json.load(fp)
return data

list1 = []

loadjson = readjson(‘market.json‘)

profile = loadjson[‘data‘][‘products‘]
str1 = ‘insert into products (id ,name,product_id,long_name,store_nums,specifics,sort,market_price,price,‘ \
‘category_id,child_cid,img,keywords,brand_id,brand_name,safe_day,safe_unit,safe_unit_desc)values‘
for k,v in profile.items():
for product in v:
str2 = ‘‘
str2 += ‘("%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s")‘\
%(str(product[‘id‘]),str(product[‘name‘]),str(product[‘product_id‘]),str(product[‘long_name‘]),str(product[‘store_nums‘]),
str(product[‘specifics‘]),str(product[‘sort‘]),str(product[‘market_price‘]),str(product[‘price‘]),str(product[‘category_id‘]),
str(product[‘child_cid‘]),str(product[‘img‘]),str(product[‘keywords‘]),str(product[‘brand_id‘]),str(product[‘brand_name‘]),
str(product[‘safe_day‘]),str(product[‘safe_unit‘]),str(product[‘safe_unit_desc‘]))
list1.append(str2)

str3 = str1+‘,‘.join(list1)+‘;‘
print(str3)
file = codecs.open(‘market.sql‘,‘wb‘,‘utf-8‘)
file.write(str3)
file.close()
print(‘ok!‘)

json数据生成mySQL语句

标签:def   name   int   key   category   ecs   SQ   product   tor   

原文地址:https://www.cnblogs.com/airapple/p/9066067.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!