[英]String returned from MySQL in Python
我尝试使用以下Python脚本从MySQL数据库检索值:
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)
但是结果总是返回为:
b'value1':b'value2'
等等
我如何摆脱b的?
我使用Python 3.2
谢谢你的帮助
有关字符串前单/双字母的更多详细信息,请参见此问题: 字符串文字前的'b'字符做什么?
如果您使用的是Python 2.x,则b
将被忽略,并且将其简单地视为一个字符串。 如果您使用的是Python 3.x,则b
前缀表示其当前为字节格式。
假设您使用的是Python 2.x,只需将config_value强制转换为字符串: print str(config_value)
您可以使用config_value.decode('utf-8')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.