Hello, I'm new with Python and running into an error trying to print data I have retrieved from a database. The code works but there are characters being printed with it that are not displayed in the database...here is my current code:

con = mysql.connect(host="silo.soic.indiana.edu", port=3306, user="XXXXXXX", passwd="XXXXXX", db="XXXXXXXXX")
cursor = con.cursor()
group = cursor.execute("select name from people_committees where personid='%s';" % govid)
grp = cursor.fetchall()
print(grp[0:])

The code is printing this:

[(b'House Committee on Armed Services',, (b'House Committee on Armed Services',), (b'House Committee on Armed Services',), (b'House Committee on Oversight and Government Reform',), (b'House Committee on Oversight and Government Reform',)]

However, I want the code to print this:
House Committee on Armed Services, House Committee on Armed Services, etc.....

Any help you could provide me on how to do this is greatly appreaciated!
The character 'b' is not in the database when I execute this statement in mysql and I don't know why it shows on python!

The b is not in string, but it is sign of binary bytes string without set encoding.

Why you produce copy of grp for printing, wouldn't it be more clear to print the grp? Or actually do for loop over items in grp.

# you had first ) missing from data
for (item,) in [(b'House Committee on Armed Services',), (b'House Committee on Armed Services',), (b'House Committee on Armed Services',), (b'House Committee on Oversight and Government Reform',), (b'House Committee on Oversight and Government Reform',)]:
    print(item)

""" Output:     
House Committee on Armed Services
House Committee on Armed Services
House Committee on Armed Services
House Committee on Oversight and Government Reform
House Committee on Oversight and Government Reform
"""

Edited 4 Years Ago by pyTony: code

I have changed my code to do this and the print results now appear like this:

b'House Committee on Armed Services' b'House Committee on Armed Services' b'House Committee on Armed Services' b'House Committee on Oversight and Government Reform' b'House Committee on Oversight and Government Reform

I still have no idea how to remove that letter 'b' from the printed results. Do I need to encode/decode somewhere prior to printing?

This seems to work with Python3

# you had first ) missing from data
for (item,) in [(b'House Committee on Armed Services',), (b'House Committee on Armed Services',), (b'House Committee on Armed Services',), (b'House Committee on Oversight and Government Reform',), (b'House Committee on Oversight and Government Reform',)]:
    print(item.decode('ascii'))

Edited 4 Years Ago by pyTony

The original poster is supposed to close the thread, when he gets satisfying help for his question. You find the button Mark Question Solved at the bottom under reply box.

This article has been dead for over six months. Start a new discussion instead.