I use Python to operate the MySQL database, the data format of the database is UTF8, I use Python to invoke the SELECT statement after the database to obtain information, and then print out the use of printing, the original Chinese data can not be displayed, the display is a string? , in order to solve this problem, I also racked my brains.
I collected a lot of information on the Internet, everyone said that Windows default format is ' GBK ', the output from the MySQL database to obtain the Chinese data, it is necessary to write:
Assume that info is the Chinese value from the database, print info.decode (' UTF-8 '). Encode (' GBK ')
Results:
But, did I try this many times or not solve the problem, or output? , it is also argued that the encoding format is passed in when the database is linked
db = MySQLdb.connect (' localhost ', ' root ', ', ' Zou ', 3306,' UTF8 ')
Results:
This is still not the expected effect, but he did not appear this time? , but has become garbled, but here the words have been moving in a good direction.
In the end, I combined the above two viewpoints to achieve the purpose ^_^
Take a look at my code:
This time look at the results of the program running:
OK, problem solved!
Python gets a question mark or garbled character in the Chinese data output in UTF8 format from the database