今天看到園友心白水撰寫的《簡單翻譯工具--必應字典第三方API使用方法》,感覺很不錯,所以用Python也寫了一個。源碼如下: 程式運行的結果如下: 請輸入英文單詞: python美音:[ 'paɪθɑn ] 英音:[ 'paɪθ(ə)n ] n. 蟒;蚺蛇Web 蟒蛇;巨蟒;派森 例句en: Se ...
今天看到園友心白水撰寫的《簡單翻譯工具--必應字典第三方API使用方法》,感覺很不錯,所以用Python也寫了一個。源碼如下:
1 import urllib.request 2 import json 3 4 serviceurl='http://xtk.azurewebsites.net/BingDictService.aspx?Word=' 5 6 while True: 7 word = input('請輸入英文單詞: ') 8 if len(word) < 1 : 9 break 10 url = serviceurl + word 11 try: 12 uh = urllib.request.urlopen(url) 13 except: 14 print('API錯誤,請重試') 15 continue 16 data = uh.read() 17 try: 18 js = json.loads(data.decode('utf-8')) 19 except: 20 js = '' 21 22 if ( js["pronunciation"] == None): 23 print("未查到匹配單詞") 24 25 else: 26 print("美音:[" ,js["pronunciation"]["AmE"],
"] 英音:[",js["pronunciation"]["BrE"],"]\n") 27 for de in js["defs"]: 28 print(de["pos"],de["def"]) 29 print("\n例句") 30 for sam in js["sams"]: 31 print("en:", sam["eng"]) 32 print("中文:", sam["chn"],"\n") 33 continue
程式運行的結果如下:
請輸入英文單詞: python
美音:[ 'paɪθɑn ] 英音:[ 'paɪθ(ə)n ]
n. 蟒;蚺蛇
Web 蟒蛇;巨蟒;派森
例句
en: Serpent blood spurted through the air, and in a minute, the huge python was dead.
中文: 蛇血在空中噴射出來,很快,大蟒蛇就死了。
en: Suggests that Python Web services implementations were not the only ones having trouble with the air fare quote service.
中文: 指出,PythonWeb服務實現並不是惟一使用機票報價服務有困難的。
en: Another idea is to require python owners to have their pets fitted with microchips.
中文: 另一個想法是讓養蛇人給他們的寵物配上微晶元。
en: A practice millennium python seeing, changes into an elegant young man, spits into the borneol in his mouth.
中文: 一修煉千年的巨蟒看見,化為一翩翩少年,將冰片吐入他口中。
en: Participants reacted to the meaning threat implicit in Python by handing out a larger notional punishment to a lawbreaker.
中文: 參與者的反應是在巨蟒中一個具有隱含意義的威脅派由一個的更大名義讓處罰者進行處罰。
en: Quite a number of Python programmers have recommended Spark to me.
中文: 很多Python程式員都向我推薦Spark。
en: Zhu went out to investigate, and there, a few paces from his door, he saw a huge python!
中文: 祝出去查看,就在門口幾步遠的地方,他看到了一條巨蟒!
en: The balls disappeared, and the couple found a lumpy-looking carpet python nearby.
中文: 球不見了,夫婦卻發現在附近有一條身形凹凸不平的地毯蟒。
en: So, in a second study they used a description of a Monty Python sketch which participants weren't told was supposed to be a joke.
中文: 因此,在第二個實驗中,他們使用了一個描述巨蟒的素描,而參與者也沒有認為這應該是一個玩笑。
en: The scripts directory contains python scripts used to install and uninstall the application.
中文: 腳本目錄包含用於安裝和卸載應用程式的python腳本。