Python processing Chinese read database output is all question marks

Source: Internet
Author: User
Tags function definition mssqlserver

Ref:http://www.cnblogs.com/zhoujie/archive/2013/06/07/problem1.html

1, Python connection MSSQL database coding problem

Python has always been poor in Chinese support, recently encountered coding problems, and there are few common solutions to solve this problem, but after the common methods have been tried, found that can be solved, the following summarizes the commonly used to support the Chinese encoding problem (these methods may be one can solve the problem, may also be multiple combinations).

(1), first of all, to ensure that the beginning of the file to add encoding settings to explain the encoding of the file

#encoding =utf-8

(2), and then, in connection with the data connection parameters, plus the character set description of the query results of the encoding, the unintended consequence may be that the query of Chinese characters are question marks

Conn=pymssql.connect (server= '. ', user= ', password= ', database= ' MyTest ', charset= ' UTF8 ')

(3), set the Python system's default encoding (for the file, this trick is almost impossible, hehe ~ ~)

Import sysreload (SYS) sys.setdefaultencoding (' UTF8 ')

Note: The above code is "UTF8", not "utf-8", I do not understand, most of the case, this does not matter, but here I tried to be "UTF8"

An example of a simple complete Python connection MSSQLServer is as follows (the PYMSSQL package has to be installed):

1 #encoding: UTF8 2 import sys 3 reload (SYS) 4 sys.setdefaultencoding (' UTF8 ') 5 import pymssql 6 try:7     conn=pymssql.co Nnect (server= '. ', user= ', password= ', database= ' MyTest ', charset= ' UTF8 ') 8     sql= "SELECT * from UserInfo" 9     Cur=conn.cursor ()     cur.execute (SQL)     Data=cur.fetchall ()     conn.close ()     print data
Except exception,e:16 print E

The results of the operation are as follows:

[(U ' 20093501 ', U ' \xb9\xf9\xbe\xb8 ', U ' \u7537 ', +, U ' \xb4\xf3\xcf\xc0 '),


(U ' 20093504 ', U ' \xc8\xce\xd3\xaf\xd3\xaf ', U ' \u5973 ', +, U ' \xc6\xaf\xc1\xc1 ')] [Finished in 0.2s]

It's not the result we want, but it's true because the result is UTF8 encoding, though it's not a question mark or garbled puzzle. This phenomenon is indeed strange, consulted a lot of experts, learned that the best result is a field query, in order to display Chinese, the entire query, will be in the format of UTF8 display.

The 14th row of data in the preceding code is the result of the entire query, and if you specify a specific field, such as print data[0][2] (which represents the value of the field in the third column of the first row of the query result), the Chinese is output.

In fact, not only the MSSQLServer database, MySQL (need to download MYSQLDB package), Sqllite (Python's own file database), MongoDB (need to download Pymongo package), etc. or is a common text file is a similar solution.

2. Parameters *args and **kwargs

This is a Python feature, similar to the parameter array in C # and C + + (param[]), allowing the function to accept dynamic, variable numbers of parameters that are known only at run time by the parameter name. If the function definition is preceded by an * sign only before the argument, all arguments passed to the function will be saved as a tuple. If the function is defined by adding two * numbers before the arguments, all the keyword arguments passed to the function will be saved as a dictionary

def foo (*args, **kwargs):    print "positional arguments is:"    print args    print "Keyword arguments is:"    Print Kwargs

Call it and see how it works:

>>> foo (1, 2, 3) positional arguments is: (1, 2, 3) Keyword arguments are:{}>>> foo (1, 2, name= ' Adrian ', F Ramework= ' Django ') positional arguments is: (1, 2) Keyword arguments are:{' framework ': ' Django ', ' name ': ' Adrian '}

Python processing Chinese read database output is all question marks

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.