Python Development Translation Program

Source: Internet
Author: User

Many do not say, on the code!!!


#!/usr/bin/env python
#coding: Utf-8
ImportUrllib,re,json


defInput_content (URL):#receive aURL (translationAIP)
whileTrue:
Content =Raw_input('Please enter the translated content:')#What the user enters
ifStr. strip (content) = ="':#If the user entered the content is empty
Continue#into the next round of circulation
elifContent = =' Q 'orContent = =' Q ':#if the input isQorQthen quit the program
Exit()
Print'\033[31mbeing translated......\033[0m]
URL = url+content#will beURLstring stitching with translated content
returnUrl#back to the end of the stitchingURL


defHTML (data):#receive the data after the translation is completed
forIinchData
RE = Re.compile (R ' translation.*?\] ')#Define matching rules for regular expressions
List_lost = Re.findall (re,i)#match the content of the translation
Lost = list_lost[0].split (': ')[1]#Remove the match to the content
result = Json.loads (lost)#Deserialize the extracted content
Printresult[0]+'\ n'#Print the results of the translation


if__name__ = =' __main__ ':
Print'\033[33minputQorQExit Translation\033[0m\ n'#Highlight Exit information

whileTrue:
Try:
URL =' http://fanyi.youdao.com/openapi.do?keyfrom=huyuanpy&key=902574943&type=data&doctype=json& Version=1.1&q= '
#Call the YoudaoAPIinterface
Address = input_content (URL)#receive post-stitching content
data = Urllib.urlopen (address)#Pass data to Youdao for translation and get the result after translation

HTML (data)#format the resulting data
except:
Print'\033[31mExiting script......\033[0m\ n'
Exit()


This article is from the "Automated Operations" blog, please be sure to keep this source http://hongchen99.blog.51cto.com/12534281/1908047

Python Development Translation Program

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.