views:

32

answers:

3
>>> c = conn.cursor()

>>> c.execute('select * from stocks order by price')
>>> for row in c:
...    print row
...

(u'2006-01-05', u'BUY', u'RHAT', 100, 35.14)

(u'2006-03-28', u'BUY', u'IBM', 1000, 45.0)

As what we can see, there's an 'u' in front of each column. And it's getting worse in my db.

How could we print the normal string?

A: 

'u' represents unicode. You can work with them just like strings. If you really want strings then do str(u'BUY').

UPDATE: I misred the question. The below code is for MySQL DB not mysqli. Sorry for the confusion. Not removing the content.

Also, just a suggestion. Instead of of just using a cursor which returns a tuple as result set. Use 'dict' as cursor. It'll give you an easy to access dictionary with field names as keys & its corresponding values as rows. This is more pythonic. This could be done like so -

cursor = conn.cursor(MySQLdb.cursors.DictCursor)

instead of

cursor = conn.cursor()

Where conn is your connection object got from MySQLdb.connect() method

MovieYoda
It says that global name 'u' is not defined when I tried
when I tried print str(u, cols)
hey! just do str(<your_value>). Also there is no ",". str(u, cols) is definitely a syntax error.
MovieYoda
Hi, it's mysqli database.
str(u'mystring'), the u goes before a string *literal*.
Xorlev
I used conn = connect('db') cur = conn.cursor() to get my conn
oh! sorry in that case nevermind.
MovieYoda
str(cols) doesn't work, same result I got
do this tuple(str(val) for val in cols).
MovieYoda
Tried tuple(str(val) for val in cols). Nothing has changed...Help~~~~
Anyone who has good idea how to solve that?
I'm sorry to give you a negative vote, but I actually consider your answer to be harmful. str(u'BUY') is not universally safe. Invoking str() with unicode input will result in using default encoder (ascii). Even a single unicode character outside this charset will result in UnicodeEncodeError.
jsalonen
hey that's what the good man wants! I am not going to argue with that!! :) Also others have suggested the same solution. That's how one converts a unicode string to string. I know the 'correct' method is to decode the string using <str_value>.decode('utf8').
MovieYoda
Did you or did you not read my answer?
jsalonen
@jsalonen I read. I am not even sure what the 'user' wants normal strings. unicode encoded strings are good to carry on with.
MovieYoda
Yeah well good point -- after re-reading the question I'm not sure about that either :/ Some more information from the question asker is totally needed for a best possible answer.
jsalonen
A: 

Convert the unicode strings in the row tuple into regular strings:

for row in c:
    print tuple(str(x) for x in row)
Amber
NO, the 'u' still in front of each field./``
+1  A: 

I get the feeling that you are not very accustomed to using Unicode. To begin with (1):

"There Ain't No Such Thing As Plain Text.

If you have a string, in memory, in a file, or in an email message, you have to know what encoding it is in or you cannot interpret it or display it to users correctly."

u in front of a text marks that you are dealing with a unicode object instead of string object. Thus, if you make a string representation of a unicode object, the u will always appear.

In order to remove the u, you need to encode the unicode object with some specific encoding. For instance, if you are using a console that has utf-8 encoding, you wish to create an utf-8 encoded string representation of your data.

Unicode objects can be encoded with encode. For instance:

print row[0].encode('utf-8')

Should result in

2006-01-05

...IF your console/terminal is using the same encoding (utf-8).

Similarly you can iterate through the tuples:

for row in c:
    print tuple(x.encode('utf-8') for x in row)

I absolutely, positively recommend, possibly even insist you to read "The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)". Unicode may be tough to begin with, but when you master the concepts, it will be your new best friend - and the more u's you will see, the happier you will get. Trust me.

jsalonen