python英文翻译

Standard

之前在网上下了个mp3格式的英语高频字汇表,带lrc字幕的.
平时没事可以一直开着加深记忆.
不过其字幕没有中文翻译,于是就写了段东西帮它翻译一下.

原字幕文件

[00:00.01]a
[00:01.728]abbreviation
[00:04.128]able
[00:05.976]about
[00:08.16]abroad
[00:09.864]absent-minded
[00:12.336]absorb

用于翻译英文单词的URL返回值
URL = ‘http://dict.cn/ws.php?utf8=true&q=%s’

<?xml version="1.0" encoding="UTF-8" ?>
<dict>
<key>a</key>
<lang>ec</lang>
<audio>http://mp3.dict.cn/mp3.php?q=JIkye</audio>
<pron>&#601;,ei</pron>
<def>art.一(个), 每一(个), 任一(个) </def>
<sent><orig>I have engaged &lt;em&gt;a&lt;/em&gt; room at this hotel.</orig><trans>我已经在这个旅馆里预订了一个房间。</trans></sent>
 
<sent><orig>You should give it &lt;em&gt;a&lt;/em&gt; try.</orig><trans>你应该试一试。</trans></sent>
<sent><orig>I don't care what &lt;em&gt;a&lt;/em&gt; car looks like as long as it gets me from A to B.</orig><trans>我倒不在乎汽车的样子,只要能把我从一处载到另一处就行了。</trans></sent>
</dict>

代码

#!/usr/bin/python
#coding=utf8
 
import urllib
import xml.dom.minidom as xml
 
URL = 'http://dict.cn/ws.php?utf8=true&q=%s'
 
def getText(nodelist):
    rc = ""
    for node in nodelist:
        if node.nodeType == node.TEXT_NODE:
            rc = rc + node.data
    return rc
 
def translate(word):
    xmls = urllib.urlopen(URL % urllib.quote(word)).read()
    root = xml.parseString(xmls).documentElement
 
    try:
        str = getText(root.getElementsByTagName("def")[0].childNodes)
    except:
        str = "NULL"
    return str.replace('\n','')
 
if __name__ == '__main__':
    r=open('english.lrc')
    wordlist=list( r.read().split('\n') )
    r.close()
 
    w=open('english_new.lrc','a')
 
    i=0
    while i<len(wordlist):
        text = wordlist[i].split(']')[0]+"]"+wordlist[i].split(']')[1] + "  " + translate( wordlist[i].split(']')[1])
        w.writelines(text.encode("utf-8")+'\n\r')
        i=i+1
 
    w.close();

翻译后的字幕

[00:00.01]a  art.一(个), 每一(个), 任一(个) 
[00:01.728]abbreviation  n.缩写 
[00:04.128]able  adj.能够的, 有能力的, 能干的 
[00:05.976]about  prep.关于, 大约, 在 ... 周围 adv.大约, 附近, 到处, 在周围 
[00:08.16]abroad  adv.到国外, 广为流传 adj.在国外, 海外(一般作表语) 
[00:09.864]absent-minded  adj. 心不在焉的
[00:12.336]absorb  vt.吸收, 吸引 ... 的注意, 吞并, 承受

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.