• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > python自动翻译实现方法

python自动翻译实现方法

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了python实现单例的方法,笨方法学python pdf,笨方法学python,笨方法学python第四版,python 静态方法等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

本文实例讲述了python自动翻译实现方法。分享给大家供大家参考,具体如下:

以前学过python的基础,一般也没用过。后来有一个参数表需要中英文。想了一下,还是用python做吧。调用的百度翻译接口,经历了乱码、模块不全等问题。一般google,一边做的。分享一下。

#encoding=utf-8
## eagle_91@sina.com
## created 2014-07-22
import urllib
import urllib2
import MySQLdb
import json
import gc
import time
url = 'http://openapi.baidu.com/public/2.0/bmt/translate'
_sleepTime = 0.5
_limit = 1000
## 链接mysql
conn = MySQLdb.connect(host='localhost',user='root',passwd='',charset='utf8')
curs = conn.cursor()
conn.select_db('test')
## 搜索要操作的表
count=curs.execute("""SELECT * FROM sb_parameters WHERE ISNULL(en_name) ORDER BY id ASC""")
## print curs.fetchall()
## print count
results = curs.fetchmany(_limit)
for r in results:
  gc.collect()
  chin = unicode(r[3]).encode('utf-8')
  ## print chin
  values = {'client_id':'PWrGllvVAIFcD0sYqaipwkAV','q':chin,'from':'zh','to':'en'}
  data = urllib.urlencode(values)
  req = urllib2.Request(url, data)
  response = urllib2.urlopen(req)
  the_page = response.read()
  returnData = json.loads(the_page)
  ## print returnData
  if returnData.has_key("error_code"):
    continue;
  inputData = returnData["trans_result"][0]["dst"]
  try:
    sql = "UPDATE `sb_parameters` SET `en_name`='%s' WHERE `id` = %d" % (MySQLdb.escape_string(unicode(inputData).encode('utf-8')),r[0])
    print sql
    curs.execute(sql)
    conn.commit()
    time.sleep(_sleepTime)
  except EOFError:
    sql = "UPDATE `sb_parameters` SET `en_name`='%s' WHERE `id` = %d" % ('',r[0])
    print sql
    curs.execute(sql)
    conn.commit()
    continue
conn.commit()
## 关闭链接
curs.close()
## 关闭数据库
conn.close()

</div>

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python图片操作技巧总结》、《Python数据结构与算法教程》、《Python Socket编程技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》

希望本文所述对大家Python程序设计有所帮助。

</div>

您可能想查找下面的文章:

  • python非递归全排列实现方法
  • python实现读取并显示图片的两种方法
  • python自动翻译实现方法
  • Python自动扫雷实现方法
  • Python实现模拟登录及表单提交的方法
  • Python自动扫雷实现方法
  • python实现计算倒数的方法
  • python实现计算倒数的方法
  • Python实现批量下载图片的方法
  • python实现自动登录人人网并采集信息的方法

相关文章

  • python列表操作使用示例分享
  • 实例讲解Python中的私有属性
  • Python读取mp3中ID3信息的方法
  • python实现的生成随机迷宫算法核心代码分享(含游戏完整代码)
  • python实现定时播放mp3
  • 用python + openpyxl处理excel2007文档思路以及心得
  • python多进程共享变量
  • python简单的函数定义和用法实例
  • Python中数字以及算数运算符的相关使用
  • Python 的内置字符串方法小结

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • Python库urllib与urllib2主要区别分析
    • Python中实现对list做减法操作介绍
    • python基础教程之对象和类的实际运用
    • Python原始字符串(raw strings)用法实例
    • Python实现快速多线程ping的方法
    • Python计算回文数的方法
    • Python实现的金山快盘的签到程序
    • Python二维码生成库qrcode安装和使用示例
    • python类和继承用法实例
    • win7安装python生成随机数代码分享

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有