2014-04-01 57 views
0

我尝试使用这个Python脚本检索从MySQL数据库值:字符串从MySQL返回在Python

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 '

etc

我该如何摆脱b's?

我使用Python 3.2

感谢您的帮助

+0

使用'SRT(CONFIG_VALUE)'来获取字符串值 – njzk2

+0

这没有按”帮助。同样的结果。 – matzr

+0

它的python3,然后呢? – njzk2

回答

0

看到这个问题的单/双字母前面的字符串更详细:What does the 'b' character do in front of a string literal?

如果您正在使用Python 2.x中,该b将被忽略,它被简单地视为一个字符串。如果您使用的是Python 3.x,b前缀表示它目前是字节格式。你使用Python 2.x的

假设,简单地把CONFIG_VALUE字符串:print str(config_value)

+0

我其实也读过这个。但我仍不清楚如何解决这个问题。 – matzr

+0

你的意思是它不起作用,或者你不明白我在暗示什么? – Matthew

+0

str()函数没有帮助 - 据我了解,因为我使用Python3。而你提到的另一个线索我基本理解,但不知道如何申请解决问题。 – matzr

0

您可以使用config_value.decode('utf-8')