This paper describes the implementation method of Python automatic translation. Share to everyone for your reference, as follows:
I've learned the basics of Python before, and it's generally useless. Later, a parameter table is required in English and Chinese. Think about it, or do it with Python. Call Baidu translation interface, has experienced garbled, module is not congruent with the problem. General Google, one side to do. Share a bit.
#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 table to manipulate Count=curs.execute ("" "Select * from Sb_parameters WHERE ISNULL (en_name) ORDER by ID ASC" ") # # Print Curs.fetchal L () # # Print countresults = 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) # # PRI NT 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 () Continueconn.commit () # # Close Link Curs.close () # # Close Database Conn.close ()