Example of a method using python

Source: Internet
Author: User
It is usually typed on a terminal. sometimes it is too difficult to open a dictionary without words, and a simple word query command is provided. The following article describes how to use python to implement the Dictionary. if you want to use python for reference, let's take a look. Preface

Since I have been using the Linux system, it is particularly difficult to support dictionaries. For those of me who have been reading English documents, I have been stuck in shells and used a proper dictionary before, I feel very good. although there is a web version, it is not supported for full-site English web pages. Simply implement one by yourself. the idea of implementing Youdao dictionary based on Python is also very simple. you can directly call Youdao api and parse the returned json.

Only use the python native library and support python2 and python3.

Sample code

#! /Usr/bin/env python #-*-coding: UTF-8-*-# API key: 273646050 # keyfrom: 11pegasus11 import jsonimport sys try: # py3 from urllib. parse import urlparse, quote, urlencode, unquote from urllib. request import urlopenworkflow T: # py2 from urllib import urlencode, quote, unquote from urllib2 import urlopen def fetch (query_str = ''): query_str = query_str.strip ("'"). strip ('"'). strip () if not query_str: query_str = 'Python' print (query_str) query = {'q': query_str} url =' http://fanyi.youdao.com/openapi.do?keyfrom=11pegasus11&key=273646050&type=data&doctype=json&version=1.1& '+ Urlencode (query) response = urlopen (url, timeout = 3) html = response. read (). decode ('utf-8') return html def parse (html): d = json. loads (html) try: if d. get ('errorcode') = 0: explains = d. get ('basic '). get ('explains') for I in explains: print (I) else: print ('unable to translate ') failed t: print ('translation error, please enter valid word ') def main (): try: s = sys. argv [1] doesn t IndexError: s = 'Python' parse (fetch (s) if _ name _ = '_ main _': main ()

Use

Paste the above code and name it youdao. py.

Modify the name mv youdao. py youdao and add the executable permission chmod a + x youdao.

Copy to/usr/local/bin. Cp youdao/usr/local/bin

When used, the words to be translated are used as the first command line parameter. if the sentence is enclosed by quotation marks.

Summary

The above is all the content of this article. I hope this article will help you in your study or work. if you have any questions, you can leave a message.

For more information about how to use python to implement the command-line Youdao dictionary, refer to PHP!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.