Simple Python Dictionary :)

摘自 http://github.com/panweizeng/home/blob/master/code/python/dict/dict.py 支持简单的Ch to En 和En to Ch我把它放在 /usr/bin/dict 1234567891011

$ dict 白痴

单词:白痴 音标: bái chī 释义:idiot idiocy 例句:他很聪明,但有时举止像是白痴。 翻译:He is intelligent, but sometimes he behaves like an idiot. 例句:我不讳言,我说过他是白痴。 翻译:I didn‘t mince matters: I said he was an idiot. 例句:真是白痴一个! 翻译:What a stupid idiot!

#!/usr/bin/python
#coding=utf8
import urllib
import sys
import os
import re
import xml.dom.minidom as xml

#API_URL = ‘http://dict.cn/ws.php?utf8=true&q=%s‘
API_URL = ‘http://dict-co.iciba.com/api/dictionary.php?w=%s‘
def getword(word):
    xmls = urllib.urlopen(API_URL%urllib.quote(word)).read()
    #print xmls
    root = xml.parseString(xmls).documentElement
    #print re.sub(u‘>‘, ‘>\n‘,xmls)

    #tags = {‘key‘:‘单词‘, ‘pron‘:‘音标‘, ‘def‘:‘释义‘, ‘sent‘:‘例句‘, ‘orig‘:‘例句‘, ‘trans‘:‘翻译‘, ‘acceptation‘:‘释义‘}
    tags = {‘key‘:‘单词‘, ‘ps‘:‘音标‘, ‘def‘:‘释义‘, ‘sent‘:‘例句‘, ‘orig‘:‘例句‘, ‘trans‘:‘翻译‘, ‘acceptation‘:‘释义‘}

    def isElement(node):
        return node.nodeType == node.ELEMENT_NODE
    def isText(node):
        return node.nodeType == node.TEXT_NODE
    def show(node, tagName=None):
        if isText(node):
            tag = tags.get(tagName, tagName)
            print ‘%s:%s‘%(tag, node.nodeValue)
        elif isElement(node) and tags.has_key(node.tagName):
            [show(i, node.tagName) for i in node.childNodes]

    [ show(i) for i in root.childNodes if isElement(i) ]

def main():
    if len(sys.argv) >= 2:
        word = ‘ ‘.join(sys.argv[1:])
        getword(word)
        os.system(‘say %s‘ % word);

    else:
        print ‘usage:dict [word]‘

if __name__ == ‘__main__‘:
    reload(sys)
    sys.setdefaultencoding(‘utf8‘)
    main()
时间: 2024-12-19 16:26:20

Simple Python Dictionary :)的相关文章

Python dictionary implementation

Python dictionary implementation http://www.laurentluce.com/posts/python-dictionary-implementation/ August 29, 2011 This post describes how dictionaries are implemented in the Python language. Dictionaries are indexed by keys and they can be seen as

46 Simple Python Exercises 16-22题

会贴出原题和答案,答案不是最优的,也反映了我的学习过程,如果有时间会更新优化的代码. Write a function filter_long_words() that takes a list of words and an integer n and returns the list of words that are longer than n. #Write a function filter_long_words() that takes a list of words # and a

46 Simple Python Exercises (前20道题)

46 Simple Python Exercises This is version 0.45 of a collection of simple Python exercises constructed (but in many cases only found and collected) by Torbj?rn Lager ([email protected]). Most of them involve characters, words and phrases, rather than

sort a Python dictionary by value

首先要明确一点,Python的dict本身是不能被sort的,更明确地表达应该是"将一个dict通过操作转化为value有序的列表" 有以下几种方法: 1. import operator x = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0} sorted_x = sorted(x.items(), key=operator.itemgetter(1)) #sorted by value sorted_x = sorted(x.items(), key=operator

python --dictionary

我们要介绍一个新的类,词典 (dictionary).与列表相似,词典也可以储存多个元素.这种储存多个元素的对象称为容器(container). 基本概念 常见的创建词典的方法: >>>dic = {'tom':11, 'sam':57,'lily':100} >>>print type(dic) 词典和表类似的地方,是包含有多个元素,每个元素以逗号分隔.但词典的元素包含有两部分,键和值,常见的是以字符串来表示键,也可以使用数字或者真值来表示键(不可变的对象可以作为键)

Hash table and Python dictionary

One of the most useful Python collections is the dictionary, which is an associative data type where you can store key-data pairs. It is implemented using hash tables. Hash table is a collection of items which are stored in such a way as to make it e

SAE 本地环境报错[python][dictionary update sequence element #0 has length 1; 2 is required]

本地搭建SAE的python环境时,总是报如下错误: 1 F:\workspace\dev\python\frikyskice\1>dev_server.py 2 Traceback (most recent call last): 3 File "C:\Python27\Scripts\dev_server.py", line 205, in <module> 4 main(options) 5 File "C:\Python27\Scripts\dev_

python : dictionary changed size during iteration

1. 错误方式 #这里初始化一个dict >>> d = {'a':1, 'b':0, 'c':1, 'd':0} #本意是遍历dict,发现元素的值是0的话,就删掉 >>> for k in d: ... if d[k] == 0: ... del(d[k]) ... Traceback (most recent call last): File "<stdin>", line 1, in <module> RuntimeE

Beginner : Simple Python test

#!/usr/local/bin/python# -*- coding: utf-8 -*-.# line of utf-8 is for multi-language# optional 4-spaces before line rule for Python. import syssys.path.append("/homes/sli/pexpect-2.3/")import pexpectimport timeimport reimport os print "Welc