Python2.7 中文字符编码,使用Unicode时,选择什么编码格式?
回复内容:
关于编码和乱码的问题,我简单讲一下。通常问这类问题的人是混淆了若干个不同的概念,并且他们自己也没有意识到自己混淆了这些概念的。
- 终端显示字符的编码(windows下终端是cmd,linux下是各种terminal,远程登录是putty或者xshell)
- shell环境的编码。比如中文版windows用的是gbk(向下兼容gb2312),大多数linux发行版使用的是utf-8(LANG=zh_CN.UTF-8)。
- 文本文件的编码。这个通常取决于你的编辑器,而且有的编辑器支持多种编码的话,你可以在文本开头位置指定编辑器使用特定编码。比如# -*- coding: utf8 -*-,vim看到这行会默认将这个脚本认定为utf-8兼容编码格式。
- 应用程序的内部编码。一个字符串,作为数据只是一个字节数组,但是作为字符的数组,就有一个解析方式。java和python的内部字符编码是utf-16,python和java都支持用不同的编码来对字节数组进行decode来得到字符数组。
拿题主的问题来解释一下。
我在ubuntu kylin中文环境下默认terminal中做了同样的实验,但是结果和题主恰好相反:


看见没有?
题主和我都没有说谎,这是为什么呢?
因为
unicode("汉字","gb2312")
“字符”可以看成是一个抽象概念,如当楼主说“汉字”,其实他意思是表达的是表示这么一个概念的两个字符。
当字符在计算机中表示的时候,需要编码成二进制(字节),于是就出现了不同的编码方式,如 GBK, UTF-8 等。如 Kenneth 展示的,“汉字”这两个字符在 GBK 中编码为 0xBABAD7D6,而在 UTF-8 中编码为 0xE6B189E5AD97。
最终显示时,则还要根据所使用的字体,把抽象的字符转化成具象的图像。
所以,楼主的第一个问题在于虽然你看到的是“汉字”的图像,但其在该脚本的源文件中的字节编码可能是任何一种——在 Windows 下是 GBK 或 GB18030 等。于是 python 看到的是一串 GBK / GB18030 编码的字节,而你试图告诉 python 这是 UTF-8 编码的,那自然报错了。
第二个问题,对 SQL Server 不熟,不过看起来原因是当你把从数据库读出的数据(字节形式,可能是 GBK 等非 Unicode 编码)放入 unit 这个变量的时候,程序错把非 Unicode 编码的字节当成 Unicode 编码解释了。那么排查思路应该是搞清楚数据在读出时是什么编码(这可能跟数据存入时的编码相关,也可能跟数据库配置有关),以及存入 unit 时程序做了哪些转换。

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...

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

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

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...
