命令行的翻译小工具--python

 1 #!/usr/bin/env python
 2 #-*- coding: utf-8 -*-
 3 'a translate module '
 4 import sys
 5 import urllib2
 6 import json
 7 def translate(word):
 8   url="http://openapi.baidu.com/public/2.0/translate/dict/simple?client_id=F1jr7p2FxnbrO2AkqRubv0uW&q="+word+"&from=en&to=zh" #百度翻译的API
 9   response=urllib2.urlopen(url)
10   result=response.read()
11   decode=json.loads(result)
12   data=""
13   if decode["errno"]==0:     #解析API返回的Json数据
14   result=decode['data']['symbols']
15   for wlist in result:
16     for mlist in wlist['parts']:
17       data+=str(mlist['part'])+'\n'
18       for tlisp in mlist['means']:
19         data+=tlisp
20       data+='\n'
21   else:
22   return "Error!"
23 
24   print data
25 if __name__=='__main__':
26 translate(sys.argv[1])

缩进好像不太好,附上链接

git hub https://github.com/Hsiny/translater

终端直接运行 t 【单词】  就可以翻译成中文

其它的功能以后添加,

英中

生词本

posted @ 2015-02-17 18:35  YeungHsin  阅读(295)  评论(0编辑  收藏  举报