Python Automatic translation implementation

Source: Internet
Author: User
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 ()


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.