当前位置: 首页 > news >正文

python 项目编号 2025821 有关于中英文数据的收集、处理

python专栏记录:



前言

批量读取单词 JSON 文件 → 解析出单词、释义、例句、短语 → 数据清洗(去掉特殊符号) → 同步更新到 MySQL 数据库。

内容

import json
import pymysql
import re
import time
from pymysql.converters import escape_string
coon = pymysql.connect(host='1.116.27.26', port=3306, user='root', passwd='LzyLyy', charset='utf8mb4', autocommit=True)
coon.select_db('rookie_word')
cur = coon.cursor()
def replaceFran(str):fr_en = [['é', 'e'], ['ê', 'e'], ['è', 'e'], ['ë', 'e'], ['à', 'a'], ['â', 'a'], ['ç', 'c'], ['î', 'i'], ['ï', 'i'],['ô', 'o'], ['ù', 'u'], ['û', 'u'], ['ü', 'u'], ['ÿ', 'y']]for i in fr_en:str = str.replace(i[0], i[1])return str
file = open('初高中词汇/GaoZhongluan_2.json','r',encoding='utf-8')
word_list = []
for line in file.readlines():words = line.strip()word_json = json.loads(words)#单词word = word_json['headWord']try:sentence = word_json['content']['word']['content']['sentence']['sentences']  # 例句sentence_list = []for se in sentence:enSe = se['sContent']enTrans = se['sCn']sen = str(enSe) + str(enTrans)sentence_list.append(sen)# 例句资源sentence_ressentence_res = ''if(len(sentence_list)>=3):for i in range(3):sentence_res += str(sentence_list[i]) + ' | ' if(i < 2) else str(sentence_list[i])else:for l in range(len(sentence_list)):sentence_res += str(sentence_list[l])+ ' | ' if(l == len(sentence_list)) else str(sentence_list[l])except:sentence_res = ''# try:#     usphone = '美[' + word_json['content']['word']['content']['usphone']+']'  # 美式音标#     usphone_er = usphone.find('(for')#     if (usphone_er != -1):#         usphone = ''# except:#     usphone = ''# try:#     ukphone = '英[' + word_json['content']['word']['content']['ukphone']+']'  # 英式音标#     ukphone_er = ukphone.find('(for')#     if (ukphone_er != -1):#         ukphone = ''# except:#     ukphone = ''# #音标pronounce# pronounce = (usphone + '|' + ukphone).replace('ˈ', '\'')   # .replace('\'', '-').replace('ˈ', '-')try:phrase = word_json['content']['word']['content']['phrase']['phrases']# 短语资源pharse_rephrase_re = ''if(len(phrase)>=4):for ph in range(4):ph_content = phrase[ph]['pContent']ph_cn = phrase[ph]['pCn']phrase_re += (str(ph_content) + ' ' + str(ph_cn))+' | ' if ph < 3 else (str(ph_content) + ' ' + str(ph_cn))else:for ph in range(len(phrase)):ph_content = phrase[ph]['pContent']ph_cn = phrase[ph]['pCn']phrase_re += (str(ph_content) + ' ' + str(ph_cn)) if ph < len(phrase) - 1 else (str(ph_content) + ' ' + str(ph_cn) + ' | ')except:phrase_re = ''trans = word_json['content']['word']['content']['trans']  # 释义# 释义资源trans_re = ''for tr in range(len(trans)):try:pos = trans[tr]['pos']except:pos = ''tranCn = trans[tr]['tranCn']try:tranOther = trans[tr]['tranOther']except:tranOther = ''trans_re += (str(pos) + ' ' + str(tranCn) + ' ' + str(tranOther)) if tr < len(trans) - 1 else (str(pos) + ' ' + str(tranCn) + ' ' + str(tranOther) + ' | ')#释义trans_re = replaceFran(trans_re)#例句sentence_res = replaceFran(sentence_res)#短语phrase_re = replaceFran(phrase_re)# print(word_json['wordRank'])# print('单词:'+word)# print('例句:' + sentence_res)# print('音标:' + str(pronounce))# print('短语:' + phrase_re)# print('释义:' + trans_re)sql = 'select * from word where word=%s'word_res = cur.execute(sql, word)if(word_res!=0):print(str(word) + '已存在')sql_l = 'select * from word_label where word=%s and label_id=%s'label_res = cur.execute(sql_l, [word, 18])if(label_res==0):sql_word_label = 'insert into word_label values(%s,%s)'cur.execute(sql_word_label, [word, 18])coon.commit()print(str(word + '已加入新标签'))time.sleep(0.5)else:print(str(word) + '标签已经满')else:sql_word = 'UPDATE word SET explain_word= %s,sentence= %s,other= %s WHERE word= %s'cur.execute(sql_word, [trans_re, sentence_res, phrase_re, word])# sql_word = 'insert into word(word, pronounce, explain_word, sentence, other, word_source) values(%s,%s,%s,%s,%s,%s)'# cur.execute(sql_word, [word, pronounce, trans_re, sentence_res, phrase_re, 0])coon.commit()# sql_label = 'insert into word_label values(%s,%s)'# cur.execute(sql_label, [word, 18])# coon.commit()time.sleep(0.1)print('单词:' + word + ' 已更新')

知识内容

数据库
文本清洗
json数据处理


总结

阿巴。。。今天又水了一天

需要json文件的联系我


致谢

靠咖啡续命的牛马,👍点赞 📁 关注 💬评论 💰打赏。


参考

[1] deepseek等ai


往期回顾

  • 无,新手上车
http://www.xdnf.cn/news/1342081.html

相关文章:

  • 数据结构之排序大全(3)
  • Python数据可视化利器:Matplotlib从入门到实战全解析
  • C ++代码学习笔记(一)
  • TDengine IDMP 运维指南(常见问题)
  • 日语学习-日语知识点小记-构建基础-JLPT-N3阶段(18):文法+单词第6回1
  • 虚幻基础:曲线
  • 基于STM32单片机的二维码识别物联网OneNet云仓库系统
  • 图--常见面试问题
  • 从源码中学习Java面向对象的多态
  • 多级缓存一致性矩阵:ABP vNext 下的旁路 / 写穿 / 写回组合实战
  • MiniGPT-4
  • FPGA 在情绪识别领域的护理应用(三)
  • 机器学习1
  • 结合 Flutter 和 Rust 的跨平台开发方案
  • Vibe Coding v.s Prompt Engineering
  • 数据库面试常见问题
  • gsplat在windows本地部署
  • Dockerfile
  • Claude Code 已支持【团队版】和【企业版】订阅
  • Webpack的使用
  • 15. 多线程(进阶2) --- CAS 和 多线程常用的类
  • Mokker AI:一键更换照片背景的AI神器
  • 粗粮厂的基于flink的汽车实时数仓解决方案
  • selenium一些进阶方法如何使用
  • K8s快速上手-微服务篇
  • 机器学习中的聚类与集成算法:从基础到应用
  • 前端视频流处理从 0 到 “能跑”:可复制 Demo+WebGL/Worker 优化,覆盖会议 / 直播 / 监控场景
  • 【尝试】在macOS上安装cvat
  • 【51单片机】【protues仿真】基于51单片机水位监测系统
  • 鸿蒙开发中的List组件详解