I try to retrieve values from a MySQL database with this Python script:
import mysql.connector
from mysql.connector import errorcode
config_file = {}
try:
cnx = mysql.connector.connect(<removed.)
except mysql.connector.Error as err:
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
print("Something is wrong with your user name or password")
elif err.errno == errorcode.ER_BAD_DB_ERROR:
print("Database does not exists")
else:
print(err)
else:
cursor = cnx.cursor()
cursor.execute("SELECT config_key, config_value FROM T_ConfigTable")
for (config_key, config_value) in cursor:
print("{}: {}".format( config_key, config_value))
config_file[config_key] = config_value
cursor.close
cnx.close
print( config_file)
But the result always comes back as:
b'value1': b'value2'
etc
How can I get rid of the b's?
I use Python 3.2
thanks for your help
See this question for more detail on single/double letters preceding strings: What does the 'b' character do in front of a string literal?
If you are using Python 2.x, the b
will be ignored and it is simply treated as a string. If you are using Python 3.x, the b
prefix means it's currently in byte format.
Assuming you're using Python 2.x, simply cast the config_value to a string: print str(config_value)
您可以使用config_value.decode('utf-8')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.