Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to view character encodings in Python 3

Shulou Source: shulou.com Published: 2022-06-02 08:21:25 10月22日 Update

This article will explain in detail how to view the character encoding in Python 3. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

First, view the web page coding

# coding=utf-8

Import urllib.request

Import chardet

Url = 'http://www.baidu.com'

A = urllib.request.urlopen (url)

Encode = chardet.detect (a.read ())

Print (encode ['encoding'])

Second, view the content coding of the file

# suppose there is a file for a.txt

F = open ('a. TXT, 'rb')

Print (chardet.detect (f.read))

Third, view a string encoding

Import chardet

S = 'Zhang San'

Print (chardet.detect (str.encode (s)

Output information: {'encoding':' utf-8', 'confidence': 0.7525,' language':'}

Tips:

When chardet.detect looks at the encoding passed by a string, it must encode the string before it can view the current string encoding format.

On how to view the character code in Python 3 to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Encoding characters strings content files articles more knowledge articles good information format web page quality Zhang San reference help related output Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Xiaomi Shulou Technology OPPO Reno Linux