您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

python处理json数据插入数据库——英语单词词库

bubuko 2022/1/25 19:12:41 python 字数 2312 阅读 789 来源 http://www.bubuko.com/infolist-5-1.html

首先从github上获取别人扒好的词库json数据 https://github.com/kajweb/dict 数据格式大致如下 接着就可以直接使用python处理数据,并插入数据库了 import sys from jsonpath import jsonpath import json imp ...

首先从github上获取别人扒好的词库json数据

https://github.com/kajweb/dict

 

数据格式大致如下

技术分享图片

 

 

接着就可以直接使用python处理数据,并插入数据库了

import sys
from jsonpath import jsonpath
import json
import demjson
import pymysql

#打开文件名为json的文件夹下的json文件
filename = "json\\cet4_2.json"
file = open(filename, ‘r‘, encoding=‘utf-8‘)

#链接数据库
def dbconnect():
    try:
        db = pymysql.connect(
            host=‘localhost‘,
            user=‘root‘,
            passwd=‘123456‘,
            db=‘vocab‘
        )
    except Exception as e:
        sys.exit("Can‘t connect to database")
    return db

#插入数据
def insertDb(word, trans, pos):
    try:
        db = dbconnect()
        cursor = db.cursor()
        cursor.execute(" INSERT INTO toefl(word, trans, pos) VALUES(%s, %s, %s)", (word, trans, pos))
        db.commit()
        cursor.close()
    except Exception as e:
        print(str(e))

#逐行读取json数据
cnt = 0
for line in file.readlines():
    words = line.strip()
    word_json = json.loads(words)
    word = ‘‘.join(jsonpath(word_json, "$..headWord"))
    trans = ‘‘.join(jsonpath(word_json, "$..tranCn"))
    res = demjson.decode(words)
    pos = ((((res.get(‘content‘)).get(‘word‘)).get(‘content‘)).get(‘trans‘))[0].get(‘pos‘)
    # print(word, trans, res, pos)
    insertDb(word, trans, pos)

file.close()

 

插入成功后

技术分享图片

 

 

 

 

 

参考资料:

https://github.com/kajweb/dict

https://www.jb51.net/article/177500.htm

python处理json数据插入数据库——英语单词词库

原文:https://www.cnblogs.com/leftstan/p/14616904.html


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶