Search code examples
pythonunicodeutf-8xbmc

how to convert from unicode to utf8 in python?


I'm working on my python script to extract the data from sqlite3 database for xbmc media application.

I can see that in my code it will extract the data using the unicode object where I will have the strings (u', u and L.

I want to convert it back to normal strings from unicode object to utf8.

Here is the code:

programs = None
daysLimit = 14
start = datetime.datetime.now()
end = start + datetime.timedelta(days = daysLimit)
cur.execute('SELECT channel, title, start_date, stop_date FROM programs WHERE channel')
programs = cur.fetchall()

print(programs)
cur.close()

Here is the xbmc log:

03:49:03 T:3628  NOTICE: [(u'101 ABC FAMILY ', u'The Middle -  The Ditch',
20140520170000L, 20140520173000L), (u'101 ABC FAMILY ', u'The Goonies', 
20140520173000L, 20140520200000L), (u'101 ABC FAMILY ', u'Pirates of the Caribbean: On Stranger Tides', 
20140520200000L, 20140520230000L), (u'101 ABC FAMILY ', u'The 700 Club', 
20140520230000L, 20140521000000L), (u'101 ABC FAMILY ', u'The Fresh Prince of Bel-Air -  Day Damn One', 
20140521000000L, 20140521003000L), (u'101 ABC FAMILY ', u'The Fresh Prince of Bel-Air -  Lucky Charm', 
20140521003000L, 20140521010000L), (u'101 ABC FAMILY ', u'The Fresh Prince of Bel-Air -  The Ethnic Tip', 
20140521010000L, 20140521013000L), (u'101 ABC FAMILY ', u'The Fresh Prince of Bel-Air -  The Young and the Restless', 
20140521013000L, 20140521020000L), (u'101 ABC FAMILY ', u'Summer Sexy With T25!', 
20140521020000L, 20140521023000L), (u'101 ABC FAMILY ', u'Paid Programming', 
20140521023000L, 20140521030000L)

I want to ignore the strings (u', u and L so I want to make it look like this:

'101 ABC FAMILY ', 'The Middle -  The Ditch', 20140520170000, 20140520173000, 
'101 ABC FAMILY ', 'The Goonies', 20140520173000, 20140520200000, 
'101 ABC FAMILY ', 'Pirates of the Caribbean: On Stranger Tides', 20140520200000, 20140520230000, 
'101 ABC FAMILY ', 'The 700 Club', 20140520230000, 20140521000000, 
'101 ABC FAMILY ', 'The Fresh Prince of Bel-Air -  Day Damn One', 20140521000000, 20140521003000,
and so on...

Can you please tell me how i can convert from unicode object to utf8 using python 2.6 version?


Solution

  • Your problem is that you are trying to display data, INSTEAD you are displaying python representation if this object.

    So it contains meta-data like u, L, etc. If you want to display data the way you want, you should write a code to deal with it.

    For example:

    for row in cur.fetchall():
        print u"'{row[0]}', '{row[1]}', '{row[2]}', '{row[3]}', '{row[4]}'".format(row=row)
    

    So it will look like

    '1', '2', '3', '4'
    '1', '2', '3', '4'
    '1', '2', '3', '4'
    

    But... as I can see, you make structure look like CSV-file(comma-separated values), do you? So, maybe, you should read about csv python module?