Fur-gan
Fur-gan

Reputation: 111

'u' at the beginning of output (Sqlite, Python)

I'm really confused about the sqlite part, where I print out my database, because I keep getting an 'u' at the beginning of every line, it doesn't have ' ' around though, which I assume is not accepted as a string? This only happens on this one programme (I tested it on other as well).

I checked the code many times (it doesn't appear in the database when I open it with 'SQLite database browser')

So this is the code:

#!/usr/bin/python

import sqlite3
import os

def list_n_convert(way):
    if way == 'mov':#movie search
        output = os.popen("find '/home/fugo/' -name '*.mp4' -printf '%f\n'").read()
    word = ''
    lyst = []
    for letter in output:
        if letter != '\n':
            word += str(letter)
        else:
            lyst.append(word)
            word = ''
    return lyst

#Loop to create entries
def entry_maker(lyst, column):
    for count in range(len(lyst)):
        cur.execute("INSERT OR IGNORE INTO myliltable ({}) VALUES (?)".format(column), (lyst[count],))
        con.commit()

if __name__ == '__main__':

    #necessities for start-up of sql-lite
    con = sqlite3.connect('movie.db')
    cur = con.cursor()

    #Create a table, if it's non-existent
    cur.execute('CREATE TABLE IF NOT EXISTS myliltable (name TEXT PRIMARY KEY, cover TEXT, genre TEXT)')

    entry_maker(list_n_convert('mov'), 'name')

    cur.execute('SELECT * FROM myliltable')
    for row in cur.fetchall():
        print(row) #print each row in a line <--- 'ERROR' happens here

    cur.close()
    con.close()

This is what the output looks like:

(u'Umbra.mp4', None, None)
(u'xd.mp4', None, None)
(u'Nice.mp4', None, None)
(u'Haha.mp4', None, None)

I'll explain what the programme does, maybe it helps to find the error:

The first function 'list_n_convert' searches on my home folder for any files with the ending '.mp4' and then converts the result of the search into strings and adds them on a list.

The second one does entries into the database for each element in the list (each mp4 file). At the end I call the functions and let each row get printed out; this is where the error happens.

Upvotes: 0

Views: 1312

Answers (1)

cssko
cssko

Reputation: 3045

The u' is basically syntax for 'this is unicode'. Use str() to get a string.

See: Unicode Strings on the python docs

Upvotes: 1

Related Questions