1 #!/usr/bin/env python2 #-*-coding:utf-8-*-3 'a translate module'4 ImportSYS5 ImportUrllib26 ImportJSON7 defTranslate (word):8Url="http://openapi.baidu.com/public/2.0/translate/dict/simple?client_id=F1jr7p2FxnbrO2AkqRubv0uW&q="+word+"&from=en&to=zh"#百度翻译的API9Response=urllib2.urlopen (URL)Tenresult=Response.read () OneDecode=json.loads (Result) AData="" - ifdecode["errno"]==0: #解析API返回的Json数据 -result=decode['Data']['symbols'] the forWlistinchResult: - forMlistinchwlist['Parts']: -Data+=str (mlist[' Part'])+'\ n' - forTlispinchmlist['means']: +data+=Tlisp -data+='\ n' + Else: A return "error!" at - PrintData - if __name__=='__main__': -Translate (sys.argv[1])
Indentation seems not very good, attached link
Git hub https://github.com/Hsiny/translater
Terminal directly run T "word" can be translated into Chinese
Other functions are added later,
UK-China
New Word Book
command-line translation gadget--python