Python automatic translation implementation, python translation implementation
This example describes 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: // limit = 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 ()