Python implements translation and python implements translation.

Source: Internet
Author: User

Python implements translation and python implements translation.

When I first introduced Python, it was attracted by her introduction syntax. The code was concise and elegant. Previously, opening a file and writing a file in C # is much more complicated than Python, in Python, it is much easier to open, modify, and save files.

1. Example of opening a file:

1 file = open ('d: \ Python \ untitled \ Hello.txt ', 'R', encoding = 'utf-8') 2 data = file. read () 3 print (data) 4 file. close ()

 

2. Use the urllib library to request a page for simple translation, request Baidu translation, send the content to be translated to Baidu as a parameter, assign the result to the parameter, and print it out:

Code:

1 import urllib. request 2 import urllib. parse 3 import json 4 5 content = input ("====== enter the content you want to translate: =====\ n") 6 7 url = 'HTTP: // fanyi.baidu.com/v2transapi' 8 data = {} 9 data ['from'] = 'zh '10 data ['to'] = 'en' 11 data ['transtype'] = 'translang '12 data ['simple _ means_flag '] = '3' 13 data ['query'] = content14 data = urllib. parse. urlencode (data ). encode ('utf-8') 15 response = urllib. request. urlopen (url, data) 16 html = response. read (). decode ('utf-8') 17 target = json. loads (html) 18 print ("translation result: % s" % (target ['Trans _ result'] ['data'] [0] ['dst '])

 

Effect

Related Article

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.