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.
- >>> Import urllib
- >>> Urlread = Lambda URL: urllib. urlopen (URL). Read ()
- >>> Import chardet
- >>> Chardet. Detect (urlread ("http://google.cn /"))
- {'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:
- Import urllib
- From chardet. universaldetector import universaldetector
- Usock = urllib. urlopen ('HTTP: // yahoo.co.jp /')
- Detector = universaldetector ()
- For line in usock. readlines ():
- Detector. Feed (line)
- If detector. Done: Break
- Detector. Close ()
- Usock. Close ()
- Print detector. Result
- {'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