小生英语较差,阅读时有很多生词,为了方便学习,将存在文件中的单词们通过python自动翻译,便于查看与记忆

文章目录

  • 可用的关于英语的HTTP服务
  • 翻译Excel中的单词并存成Excel
  • 翻译txt中的单词并存成Excel

可用的关于英语的HTTP服务

  1. 有道词典单词发音
    http://dict.youdao.com/dictvoice?audio=%s

  2. 有道词典翻译(单词、句子)
    收费,有100体验金
    需要申请Key:http://ai.youdao.com/

  3. 金山毒霸
    http://dict-co.iciba.com/api/dictionary.php?w=%s&key=key
    需要申请Key:http://open.iciba.com/?c=api

  4. 扇贝单词发音
    美式:http://media.shanbay.com/audio/us/%s.mp3
    英式:http://media.shanbay.com/audio/uk/%s.mp3

翻译Excel中的单词并存成Excel

Excel约定格式:

  1. 需要翻译,就在单词的单元格后留一个空位
  2. 需要音标,就在单词的单元格后留一格空位
  3. 不是单词的单元格,后一格需要有值
    如,需要翻译和音标,则在单词后面留了两个单元格;3月7日(单元格A1)后的单元格(B1)有值

这里使用金山毒霸的API,KEY这里就不给出了

# coding:utf-8
import os
import openpyxl
import urllib
from bs4 import BeautifulSoupinput_path = u'E:\我的坚果云\英语单词.xlsx'
GET_ALL_MEANING = True #得到单词的所有意思
GET_MEANING = 1 # 获得单词意思,0 1
GET_PHONETIC = 1 #获得单词音标,0 1KEY = 'B84BCE38D6CF06***48FC4A6DE' #金山毒霸的API
def search(word):result = {}url_prefix = 'http://dict-co.iciba.com/api/dictionary.php?%s'url_param_json = {}url_param_json['key'] = KEYurl_param_json['w'] = wordurl_param = urllib.urlencode(url_param_json)url = url_prefix % url_paramprint "查询的Url:%s" % urlhtml = urllib.urlopen(url)dom = BeautifulSoup(html,'xml')result['meaning'] = ""acceptations = dom.findAll('acceptation') #单词意思poss = dom.findAll('pos') #词性a_len = len(acceptations)p_len = len(poss)if GET_ALL_MEANING:for i in range(0,min(a_len,p_len)):result['meaning'] += poss[i].get_text() + " " + acceptations[i].get_text()else:if a_len>0 and p_len>0:result['meaning'] += " " + poss[0].get_text() + " " + acceptations[0].get_text()result['meaning'] = result['meaning'][0:-1]# 音标result['ps'] = ""pss = dom.findAll('ps')for ps in pss:result['ps'] += '[' + ps.get_text() + ']'return resultif __name__ == '__main__':excel = openpyxl.load_workbook(input_path)sheet_names = excel.get_sheet_names()name = sheet_names[0]sheet = excel.get_sheet_by_name(name)col_step = 1 + GET_MEANING + GET_PHONETICfor nrow,row in enumerate(sheet.rows):for ncol in range(0,len(row),col_step):if row[ncol].value == None: # 单词位无值breakif ncol==0 and row[1].value != None: #单词位后一格有值breakprint row[ncol].valueresult = search(row[ncol].value)index = ncolif GET_MEANING==1: #获取意思index = index + 1sheet.cell(row = nrow,column=index).value = result['meaning']if GET_PHONETIC==1:index = index + 1sheet.cell(row=nrow, column=index).value = result['ps']excel.save(input_path)

结果:

翻译txt中的单词并存成Excel

上个版本需要按规则存入Excel挺麻烦的
这里只需要将单词按规则写入txt,选择分隔符就好了

txt文件示例:以’\n’分隔符

代码:

# coding:utf-8
import os
import xlwt
import urllib
from bs4 import BeautifulSoupfile_path = u'D:\\Users\\PasserQi\\Desktop\\4月8日--2017年12月2选段落.txt'
out_dir = u'D:\\Users\\PasserQi\\Desktop'
SEPARATOR = '\n'#分割符
GET_ALL_MEANING = True #得到单词的所有意思
GET_MEANING = 1 # 获得单词意思,0 1
GET_PHONETIC = 1 #获得单词音标,0 1
LINE_NUMBER = 5 #一行存5个# translate by http service
KEY = 'B84BCE38D6CF06A****C5048FC4A6DE'
def search(word):result = {}url_prefix = 'http://dict-co.iciba.com/api/dictionary.php?%s'url_param_json = {}url_param_json['key'] = KEYurl_param_json['w'] = wordurl_param = urllib.urlencode(url_param_json)url = url_prefix % url_paramprint "查询的Url:%s" % urlhtml = urllib.urlopen(url)dom = BeautifulSoup(html,'xml')result['meaning'] = ""acceptations = dom.findAll('acceptation') #单词意思poss = dom.findAll('pos') #词性a_len = len(acceptations)p_len = len(poss)if GET_ALL_MEANING:for i in range(0,min(a_len,p_len)):result['meaning'] += poss[i].get_text() + " " + acceptations[i].get_text()else:if a_len>0 and p_len>0:result['meaning'] += poss[0].get_text() + " " + acceptations[0].get_text()result['meaning'] = result['meaning'][0:-1]# 音标result['ps'] = ""pss = dom.findAll('ps')for ps in pss:result['ps'] += '[' + ps.get_text() + ']'return resultdef save_to_excel(words):wb = xlwt.Workbook(encoding='utf-8')sheet = wb.add_sheet('Sheet 1')col_step = 2 + GET_MEANING + GET_PHONETICrow = 0col = 0for word,value in words.items():index = colsheet.write(row,index,word)if GET_PHONETIC==1:index = index + 1sheet.write(row,index,value['ps'])if GET_MEANING == 1:  # 获取意思index = index + 1sheet.write(row,index,value['meaning'])col += col_stepif (col+1)/col_step == LINE_NUMBER : #一行存5个row += 1col = 0filename = os.path.split(file_path)[1].split('.')[0]wb.save(os.path.join(out_dir,filename+'.xls'))if __name__ == '__main__':# open filef = open(file_path)dom = f.read()f.close()# translate wordswords = {}list = dom.split(SEPARATOR)for word in list:words[word] = search(word)# save wordssave_to_excel(words)

结果:
以txt文件名为excel的文件名

[Python] 调用接口自动翻译单词,并存入文件相关推荐

  1. 【Python】解决Python调用接口返回带菱形问号和乱码

    现象: python调用接口Print(response.text)返回菱形问号或乱码时, 解决方法: 请求头应该添加"Accept-Encoding":"deflate ...

  2. python调用接口上传文件_python上传文件接口

    文件的参数组装: ('文件名',"open打开的文件(rb模式打开)",'文件的类型说明') 关于不同的请求参数类型,使用requests的处理: 1.文件上传(Content-T ...

  3. yolov4-tiny从安装到训练再到python调用接口

    (一)安装 在GitHub网址https://github.com/AlexeyAB/darknet下载最新版的darknetAB源码 解压后会生成名为darknet-master的文件夹 将解压的文 ...

  4. python调用接口获取文件_python接口文件使用说明

    首先, python 接口文件在安装好的 darknet 目录下的 python 文件夹,打开就 可以看到 这里的 darknet.py 文件就是 python 接口 用编辑器打开查看最后部分代码: ...

  5. python调用接口上传文件_python接口自动化7-post文件上传

    前言 文件上传在我们软件是不可少的,最多的使用是体现在我们后台,当然我们前台也会有.但是了解过怎样上传文件吗?这篇我们以禅道文档-创建文档,上传文件为例. post请求中的:Content-Type: ...

  6. python 调用接口发送短信给手机(非twilio)

    python发短信给手机 这篇文章将会持续更新: 今天先给两种可以成功发送短信的方法,都是调用第三方接口的方式发送短信, 上次写过twilio的发送:https://editor.csdn.net/m ...

  7. python调用接口获取数据_python:接口间数据传递与调用方法

    如下所示: import requests import unittest import json from pubulic_way.get_token import getSession class ...

  8. python调用接口查询_基于Python的苹果序列号官网查询接口调用代码实例

    1.[代码][Python]代码 #!/usr/bin/python # -*- coding: utf-8 -*- import json, urllib from urllib import ur ...

  9. python调用接口时传多个参数_python接口自动化11-post传data参数案例

    前言: 前面登录博客园的是传json参数,有些登录不是传json的,如jenkins的登录,本篇以jenkins登录为案例,传data参数. 一.登录jenkins抓包 1.登录jenkins,输入账 ...

  10. python调用接口requests_【python接口自动化】- 使用requests库发送http请求

    前言:什么是Requests ?Requests 是⽤Python语⾔编写,基于urllib,采⽤Apache2 Licensed开源协议的 HTTP 库.它⽐ urllib 更加⽅便,可以节约我们⼤ ...

最新文章

  1. 信号状态关_路由器要不要关?难怪信号越来越差
  2. cnn 准确率无法提升_清华类脑芯片再登Nature: 全球首款基于忆阻器的CNN存算一体芯片...
  3. 【优先队列】HDU 1873——看病找医生
  4. 多旋翼无人机动力系统发展历程
  5. css 识别屏幕大小自适应
  6. 图像去噪(阿尔法均值滤波器)
  7. 「击败星际争霸II职业玩家」的 AlphaStar是在作弊?
  8. 【Unity入门计划】Unity2D动画(1)-动画系统的组成及功能的使用
  9. 全国降雨量数据、气温数据、风速数据
  10. PHP 递归函数的三种实现方式
  11. 【HTML5 笔记】基础内容
  12. android背光系统,Android 的背光控制
  13. 【数据库】逻辑设计-ER模型转换为关系模型
  14. 【HISI系列】之HISI芯片自带硬件算法模块
  15. 计算机毕业设计基于asp.net学生成绩管理信息系统
  16. C++头文件和std命名空间(精辟)
  17. Python制作GUI钢琴,几行代码完成一个游戏项目
  18. html 兼容ie11,前端开发中在IE11下出现的一些兼容性问题(持续汇总)
  19. android 软件开发 小米 魅族 htc 测试中的问题,iOS和Android-农行手机银行常见问题解决方法-K宝篇...
  20. 在UCOSIII基础上对STM32F407ZGT6移植LWIP2.1.3

热门文章

  1. Windows任务栏图标变白解决方案
  2. Flink CheckPoint : Exceeded checkpoint tolerable failure threshold
  3. 基金暴跌年轻人为什么躲不过被割?
  4. Logisim海明编码解码实验
  5. Runtime Error! R6025-pure virtual function call 问题怎么解决
  6. 【愚公系列】2022年07月 Go教学课程 004-Go代码注释
  7. coldfusion_ColdFusion教程:第一部分
  8. #vue#element-ui文件上传(格式校验)
  9. OpenCV黑魔法之隐身衣 | 附源码
  10. PHP TP模板下的微博登录(wap)