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