简体   繁体   中英

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

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

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)

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)

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('}'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM