How to remove curly bracket from the output in python?

You are inserting a list or tuple into the listbox instead of a string. You need to explicitly convert each row to a string before passing to the insert method. Otherwise, the underlying tcl interpreter will try to preserve the list-like structure of the data by adding curly braces or backslashes when converting the value to a string.

Consider this code:

query = ('''SELECT * FROM Employee;''' )
cur.execute(query)
row = cur.fetchall()

row a list of lists. For each item in row, you have a list of values for each column in that row.

Later, you add each row to the listbox like this:

for x in row:
    listbox.insert(END,x)

As stated earlier, x is a list of data for each column. However, listbox.insert takes a string. Since it is not a string, tkinter will convert it to a string using its own logic which may result in curly braces being added.

To avoid this, you should explicitly convert the list to a string before calling insert. The simplest way to do that is to add a space between each element:

listbox.insert(END, " ".join(x))

If “x” contains something other than strings, you’ll need to convert them to strings first. The final version might look something like this:

string_data = [str(data) for data in x]
listbox.insert(END, " ".join(string_data))

Leave a Comment