Created
December 18, 2012 13:46
-
-
Save cloverstd/4328090 to your computer and use it in GitHub Desktop.
A terminal English Word Dict.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding:utf-8 -*- | |
import json, urllib,sys | |
script = sys.argv[0] | |
dict_url = "http://dict.qq.com/dict?q=" | |
def usage(): | |
print "Usage:" | |
print "\t%s word" % script | |
if len(sys.argv) != 2: | |
usage() | |
exit() | |
word = sys.argv[1] | |
word_dict_json = urllib.urlopen(dict_url + word).read() | |
if word_dict_json == '{"err":"sorry, no result"}': | |
print "Not find '%s''s mean" % word | |
exit() | |
word_dict = json.loads(word_dict_json) | |
if word_dict.has_key('local'): | |
pass | |
else: | |
print "Sorry!\nCan not query" | |
exit() | |
print "%s" % word | |
try: | |
if word_dict['lang'] == 'eng': | |
# words is a list | |
words = word_dict['local'][0]['des'] | |
for mean in words: | |
for k in mean.keys(): | |
print "%s" % (mean[k]), | |
print "\n" | |
elif word_dict['lang'] == 'ch': | |
words = word_dict['local'][0]['des'] | |
for mean in words: | |
print "%s" % mean | |
except KeyError: | |
print "Sorry!\nCan not query" | |
exit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment