Python Automatic translation implementation

Source: Internet
Author: User
This article mainly introduces the python Automatic translation implementation method, which involves the skills related to Python's call to Baidu interfaces and database operations. if you need it, refer to the example below to describe how to implement python automatic translation. We will share this with you for your reference. The details are as follows:

I have learned the basics of python before and have never used it. Later, a parameter table must be in Chinese and English. Think about it, but use python. The Baidu translation interface has encountered garbled code, incomplete modules, and other problems. Generally, google does this. Share it.

# Encoding = UTF-8 # eagle_91@sina.com # created 2014-07-22import urllibimport urllib2import MySQLdbimport jsonimport gcimport timeurl =' http://openapi.baidu.com/public/2.0/bmt/translate '_ SleepTime = 0.5 _ limit = 1000 # link mysqlconn = MySQLdb. connect (host = 'localhost', user = 'root', passwd = '', charset = 'utf8') curs = conn. cursor () conn. select_db ('test') # search the count‑curs.exe cute ("" SELECT * FROM sb_parameters where isnull (en_name) order by id ASC "") # print curs. fetchall () # print countresults = curs. fetchmit (_ 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.exe cute (SQL) conn. commit () time. sleep (_ sleepTime) failed t EOFError: SQL = "UPDATE 'sb _ parameters 'set 'en _ name' = '% s' WHERE 'id' = % d" % ('', r [0]) print SQL curs.exe cute (SQL) conn. commit () continueconn. commit () # Close the link curs. close () # close database conn. close ()


For more articles about python auto-translation implementation, refer to PHP Chinese network!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.