Python plug-ins (1) -- chardet

Source: Internet
Author: User

 PythoN is a widely used and widely used language. It has attracted countless developers. I have been using python for more than a month and have been familiar with many Python plug-ins.Article, Only record.
The first one is aboutChardet.ChardetTo detectPythonA plug-in developed for character set issues.

    1. >>> Import urllib
    2. >>> Urlread = Lambda URL: urllib. urlopen (URL). Read ()
    3. >>> Import chardet
    4. >>> Chardet. Detect (urlread ("http://google.cn /"))
    5. {'Encoding': 'gb2312', 'confidence ': 0.99}

CopyCode

First, let's introduce it,UrllibIs a plug-in for obtaining webpage content in Python. You can use urllib. Open (URL). Read () to obtain the webpage content.LambdaI think everyone understands that it is a basic Python syntax, a bit like a Java anonymous function.
Get down to business, useChardet. Detect ()You can "Guess" the encoding of character segments in a memory segment. Why is speculation because the character set and character set are not all different. But some are the same, and some are different. Whether it is vim or Emacs, it depends on guesses when viewing text encoding. When you look at the python output, you can see that it thinks the probability is 99%. This character set is gb2312.
The following is an advanced application:

    1. Import urllib
    2. From chardet. universaldetector import universaldetector
    3. Usock = urllib. urlopen ('HTTP: // yahoo.co.jp /')
    4. Detector = universaldetector ()
    5. For line in usock. readlines ():
    6. Detector. Feed (line)
    7. If detector. Done: Break
    8. Detector. Close ()
    9. Usock. Close ()
    10. Print detector. Result
    11. {'Encoding': 'euc-JP ', 'confidence': 0.99}

Copy code

 

The application background is that frequent calls to the detect () method will reduce the efficiency if a large amount of detection is required. At this time, you can use the global detect, that is, to continuously use the feed () method to add strings in a loop manner to achieve the lowest efficiency requirement, the detect () function will be called to detect andUniversaldetectorThe done of the object is set to true. In this case, you can view the result attribute to know the result!

The above is my experience with chardet and I hope it will help you. If you have any questions, please read the English document. The specific address is here: chardet document.
Finally, we provide the plug-ins ,. After decompression, run the setup. py. command in the folder as follows:
Python setup. py install
OK. Finally, I wrote a post. Do you have any help? If so, please give me a reply and give me a small encouragement. Thank you!

Reprinted article, please indicate from: miqu.com

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.