0

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

Lukasz Koziara
  • 4,274
  • 5
  • 32
  • 43
matzr
  • 81
  • 6

2 Answers2

1

You can use config_value.decode('utf-8')

Peeyush
  • 686
  • 4
  • 10
0

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)

Community
  • 1
  • 1
Mdev
  • 2,440
  • 2
  • 17
  • 25
  • I actually did read this. But I'm still unclear on how to solve the issue. – matzr Apr 02 '14 at 18:24
  • You mean it doesn't work or you don't understand what I'm suggesting? – Mdev Apr 02 '14 at 19:18
  • The str() function doesn't help - as far as I understand because I use Python3. And the other thread you mentioned I basically understood but don't know how to apply to solve the problem. – matzr Apr 03 '14 at 06:52
  • Any other ideas for Python3? – matzr Apr 11 '14 at 12:49