Python character encoding judgment method
The example in this article describes the Python character encoding judgment method. Share it with everyone for your reference, the details are as follows:
Method 1:
isinstance(s, str) is used to determine whether it is a general string
isinstance(s, unicode) is used to determine whether it is unicode
or
if type(str).__name__!="unicode": str=unicode(str,"utf-8") else: pass
Method 2 :
Python chardet character encoding judgment
Using chardet can easily implement string/file encoding detection. Especially for Chinese web pages, some pages use GBK/GB2312, and some use UTF8. If you need to crawl some pages, it is important to know the web page encoding. Although HTML pages have charset tags, sometimes they are incorrect. Then chardet can help us a lot.
chardet instance
>>> import urllib >>> rawdata = urllib.urlopen('http://www.google.cn/').read() >>> import chardet >>> chardet.detect(rawdata) {'confidence': 0.98999999999999999, 'encoding': 'GB2312'} >>>chardet可以直接用detect函数来检测所给字符的编码。函数返回值为字典,有2个元数,一个是检测的可信度,另外一个就是检测到的编码。
chardet installation
After downloading chardet, unzip the chardet compressed package, place the chardet folder directly in the application directory, and then use import chardet to start using chardet.
Or use the setup.py installation file to copy chardet to the Python system directory, so that all your python programs only need to import chardet.
python setup.py install reference
chardet official website: http://chardet.feedparser.org/
chardet download page: http://chardet.feedparser.org/download/
For more articles related to Python character encoding judgment methods, please pay attention to the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

Regular expressions are powerful tools for pattern matching and text manipulation in programming, enhancing efficiency in text processing across various applications.

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

The article discusses popular Python libraries like NumPy, Pandas, Matplotlib, Scikit-learn, TensorFlow, Django, Flask, and Requests, detailing their uses in scientific computing, data analysis, visualization, machine learning, web development, and H

In Python, how to dynamically create an object through a string and call its methods? This is a common programming requirement, especially if it needs to be configured or run...
