Skip to content

Instantly share code, notes, and snippets.

@cloverstd
Created December 18, 2012 13:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cloverstd/4328090 to your computer and use it in GitHub Desktop.
Save cloverstd/4328090 to your computer and use it in GitHub Desktop.
A terminal English Word Dict.
#!/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