python一直对中文支持的不好,最近老遇到编码问题,而且几乎没有通用的方案来解决这个问题,但是对常见的方法都试过之后,发现还是可以解决的,下面总结了常用的支持中文的编码问题(这些方法中可能其中一个就能解决问题,也可能是多个组合)。
(1)、首先,要保证文件的开头要加上编码设置来说明文件的编码
#encoding=utf-8
(2)、然后,在连接数据的连接参数里加上字符集说明查询出的结果的编码,这个不加的后果可能是查询出的汉字字符都是问号
conn=pymssql.connect(server='.',user='', password='',database='MyTest',charset='utf8')
(3)、设置python系统的默认编码(对于文件来说,这招几乎屡试不爽,呵呵~~)
import sys
reload(sys)
sys.setdefaultencoding('utf8')
注意:上述编码是“utf8”,而不是“utf-8”,我也没弄明白,大部分情况下,这个无所谓的,但是这里我试了必须要是“utf8”
一个简单的完整的python连接mssqlserver的例子如下(得安装pymssql包):
#encoding:utf8
import sys
reload(sys)
sys.setdefaultencoding('utf8')
import pymssql
try:
conn=pymssql.connect(server='.',user='', password='',database='MyTest',charset='utf8')
sql="select * from UserInfo"
cur=conn.cursor()
cur.execute(sql)
data=cur.fetchall()
conn.close()
print data
except Exception,e:
print e
运行结果如下:
[(u'20093501', u'\xb9\xf9\xbe\xb8', u'\u7537 ', 35, u'\xb4\xf3\xcf\xc0'),
(u'20093502', u'\xbb\xc6\xc8\xd8', u'\u5973 ', 34, u'\xc3\xc0\xc5\xae'),
(u'20093503', u'\xc1\xee\xba\xfc\xb3\xe5', u'\u7537 ', 25, u'2B\xc7\xe0\xc4\xea'),
(u'20093504', u'\xc8\xce\xd3\xaf\xd3\xaf', u'\u5973 ', 24, u'\xc6\xaf\xc1\xc1')]
[Finished in 0.2s]
虽然摆脱了问号和乱码的困扰,但这仍不是我们想要的结果,但这个确实是正确的,因为结果是utf8编码。这个现象确实诡异,请教了许多高手,得知,最好的结果就是逐个字段查询,才能显示中文,整个查询的话,会以utf8的格式显示出来。
上述代码中第14行data是整个查询的结果,如果指定某个具体的字段,如print data[0][2](表示取查询结果的第一行第三列的字段的值),则会输出中文。
其实不仅仅是mssqlserver数据库,mysql(需下载MySQLdb包)、sqllite(python自带的文件数据库)、mongodb(需下载PyMongo包)等或者是普通文本文件也是类似的解决方案。

Python and C each have their own advantages, and the choice should be based on project requirements. 1) Python is suitable for rapid development and data processing due to its concise syntax and dynamic typing. 2)C is suitable for high performance and system programming due to its static typing and manual memory management.

Choosing Python or C depends on project requirements: 1) If you need rapid development, data processing and prototype design, choose Python; 2) If you need high performance, low latency and close hardware control, choose C.

By investing 2 hours of Python learning every day, you can effectively improve your programming skills. 1. Learn new knowledge: read documents or watch tutorials. 2. Practice: Write code and complete exercises. 3. Review: Consolidate the content you have learned. 4. Project practice: Apply what you have learned in actual projects. Such a structured learning plan can help you systematically master Python and achieve career goals.

Methods to learn Python efficiently within two hours include: 1. Review the basic knowledge and ensure that you are familiar with Python installation and basic syntax; 2. Understand the core concepts of Python, such as variables, lists, functions, etc.; 3. Master basic and advanced usage by using examples; 4. Learn common errors and debugging techniques; 5. Apply performance optimization and best practices, such as using list comprehensions and following the PEP8 style guide.

Python is suitable for beginners and data science, and C is suitable for system programming and game development. 1. Python is simple and easy to use, suitable for data science and web development. 2.C provides high performance and control, suitable for game development and system programming. The choice should be based on project needs and personal interests.

Python is more suitable for data science and rapid development, while C is more suitable for high performance and system programming. 1. Python syntax is concise and easy to learn, suitable for data processing and scientific computing. 2.C has complex syntax but excellent performance and is often used in game development and system programming.

It is feasible to invest two hours a day to learn Python. 1. Learn new knowledge: Learn new concepts in one hour, such as lists and dictionaries. 2. Practice and exercises: Use one hour to perform programming exercises, such as writing small programs. Through reasonable planning and perseverance, you can master the core concepts of Python in a short time.

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.


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

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software