How do I get rid of the "{}" in my windows output? (Python / SQLite3)

Question:

I’m creating a flashcard game to ask CompSci questions.

I’m trying to retrieve a random "CardFront" which acts as a varchar stored in an SQLite3 DB table, and output that result to a messagebox to "Prompt" the user with the question.

Only problem I can’t seem to figure out is why it is returning with squiggly brackets around the statement?

from tkinter import *
import sqlite3
from tkinter import messagebox

def retrieve_random_cardfront():
    conn = sqlite3.connect('flashcards.db')
    cursor = conn.cursor()
    cursor.execute("SELECT CardFront FROM FLASHCARDS ORDER BY RANDOM() LIMIT 1;")
    result = cursor.fetchall()
    conn.close()
    messagebox.showinfo(title='Test', message=result[0])

Current Output

Asked By: Chris

||

Answers:

You can try this as a fix, but what is happening is unclear here. Maybe check into the __repr__ of the result object ?

from tkinter import *
import sqlite3
from tkinter import messagebox

def retrieve_random_cardfront():
    conn = sqlite3.connect('flashcards.db')
    cursor = conn.cursor()
    cursor.execute("SELECT CardFront FROM FLASHCARDS ORDER BY RANDOM() LIMIT 1;")
    result = cursor.fetchall()
    conn.close()
    messagebox.showinfo(title='Test', message=str(result[0]).strip('{').strip('}'))

I think you can implement something like this in order to remove that "{}". But since I’m not aware of using SQLite, I could give you a more precise answer.

data = "{Data}"
data = data.replace(data[0], "")
data = data.replace(data[-1], "")
print(data)
Answered By: Dante Zulli

tkinter will add curly braces when it expects a string but you pass a list. The proper solution is to convert your list to a string before using it with a widget. The following example shows once possible way:

message = ",".join(result[0])
messagebox.showinfo(title='Test', message=message)
Answered By: Bryan Oakley
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.