怎么办?上万事OK网

问题:如何将json文件转换为MySQL数据

【答案】:import json

data = []
with open('BaseCourseInfo.json',encoding='utf-8') as f:
    for line in f:
        data.append(json.loads(line))

import datetime
import random
str = "\r\n"
for item in data:
    #print json.dumps(item)

    # if('_class' not in item):
    #
    #     str = str + "('%s','%s','%s','%s');\r\n" % (item['_id'], item['_name'], tt, item['_identify'])
    # else:
    # timeStamp = int(item['_class_time']['$date']['$numberLong'])
    # dateArray = datetime.datetime.fromtimestamp(timeStamp/1000, pytz.timezone('UTC'))
    # otherStyleTime = dateArray.strftime("%Y-%m-%d %H:%M:%S")
    # print(otherStyleTime)
    for i in item['_stu_set']:
        str = str + "insert into basestudents(base_course_id,stu_id,grade) values "
        str = str + "('%s','%s', '%s');\r\n" % (item['_id']['$oid'],i['_id'],random.randint(0,100))

import codecs
file_object = codecs.open('tencent.sql', 'w' ,"utf-8")
file_object.write(str)
file_object.close()
print("success")

【关键字】:

首页 > IT » 如何将json文件转换为MySQL数据

相关推荐

首页 |  家庭 | 升学 | 小学 | 中考 | 高考 | 考研 | 养老 | 育儿 | 旅游 | 宠物 | 美丽 | 家居 | 兴趣 | 健康 | 美食 | 商业 | 交通 | 艺术 | IT | 教育 | 体育 | 
© 2020 怎么办?上万事OK网