简体   繁体   中英

How to remove curly bracket from the output in python?

I am new to Python I have recently start to learn 1 week ago and I am stuck here... Any help will be appreciate...

from tkinter import *
import tkinter as tk
import psycopg2

root = Tk()

def get_info(Employee_Name, Department, Education,Salary):
   con = psycopg2.connect(dbname = 'postgres',user = 'postgres',password = 'Gaurav@31',host = 'localhost',port = 5432)
   cur = con.cursor()
   query = ('''INSERT INTO Employee (Employee_name, Department, Education,Salary) VALUES (%s,%s,%s,%s);''' )
   cur.execute(query,(Employee_Name, Department, Education,Salary))
   print('Data inserted Successfully')
   con.commit()
   con.close()
   display_all()

def search(emp_id):
    con = psycopg2.connect(dbname = 'postgres',user = 'postgres',password = 'Gaurav@31',host = 'localhost',port = 5432)
    cur = con.cursor()
    query = ('''SELECT * FROM EMPLOYEE WHERE emp_id = %s;''' )
    cur.execute(query,(emp_id))
    row = cur.fetchone()
    display_search(row)
    con.commit()
    con.close()

def display_search(row):
    listbox = Listbox(frame, width =50 , height=1)
    listbox.grid(row = 9 , column =1)
    listbox.insert(END,row)

def display_all():
    con = psycopg2.connect(dbname = 'postgres',user = 'postgres',password = 'Gaurav@31',host = 'localhost',port = 5432)
    cur = con.cursor()
    query = ('''SELECT * FROM Employee;''' )
    cur.execute(query)
    row = cur.fetchall()

    listbox = Listbox(frame, width =50 , height=8)
    listbox.grid(row = 10 , column =1)

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

canvas = Canvas(root, height = 480, width = 900)
canvas.pack()

frame = Frame()
frame.config(background="Black")
frame.place(relx = 0, rely = 0, relwidth = 1, relheight = 1)

label = Label(frame,text = " Employee Database")
labelfont = ('times', 24, 'bold')
label.config(bg = 'Black',fg ='Yellow',font =labelfont)
label.grid(row=0,column=1,sticky = N)


label = Label(frame, text = 'Employee Name : ')
labelfont1 = ('times', 18, 'bold')
label.config(bg = 'Black',fg ='Yellow',font =labelfont1)
label.grid(row =1, column = 0,sticky='w')
entry_name = Entry(frame,width = 30)
entry_name.grid(row = 1, column= 1)

label = Label(frame, text = 'Department : ')
labelfont1 = ('times', 18, 'bold')
label.config(bg = 'Black',fg ='Yellow',font =labelfont1)
label.grid(row =2, column = 0, sticky ='w')
dept_name = Entry(frame,width = 30)
dept_name.grid(row = 2, column= 1)

label = Label(frame, text = 'Education : ')
labelfont1 = ('times', 18, 'bold')
label.config(bg = 'Black',fg ='Yellow',font =labelfont1)
label.grid(row =3, column = 0,sticky ='w')
education = Entry(frame,width = 30)
education.grid(row = 3, column= 1)

label = Label(frame, text = 'Salary : ')
labelfont1 = ('times', 18, 'bold')
label.config(bg = 'Black',fg ='Yellow',font =labelfont1)
label.grid(row =4, column = 0, sticky ='w')
salary = Entry(frame,width = 30)
salary.grid(row = 4, column= 1)

button = Button(frame, text = 'Add Information', command=lambda:         get_info(entry_name.get(),dept_name.get(),education.get(),salary.get()))
buttonfont1 = ('times', 13, 'bold')
button.config(bg = 'Black',fg ='Yellow',font =buttonfont1)
button.grid(row = 5 , column= 1)

label = Label(frame, text = 'Search by Emp_id : ')
labelfont1 = ('times', 14, 'bold')
label.config(bg = 'Black',fg ='Yellow', font =labelfont1)
label.grid(row = 6, column = 0, sticky ='w')
id_search = Entry(frame,width = 30)
id_search.grid(row = 6, column= 1)

button = Button(frame, text = 'Search',command = lambda:     search(id_search.get()))
buttonfont1 = ('times', 13, 'bold')
button.config(bg = 'Black',fg ='Yellow',font =buttonfont1)
button.grid(row = 7 , column= 1)

display_all()

root.mainloop()

Please find the attached code as well as Screenshot :

I will to remove the curly bracket which is visible on in the listbox, As well as I wish to print this in a tabular form

在此处输入图片说明

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))

Bryan's explanation was incredibly helpful!!! However, when I used the code suggested,

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

my output was one long line. So I added a for loop,

item = [str(data) for data in backend.view()]
for x in item:
    lb.insert(END, "".join(x)) # don't include space between quotes

and it looks a little better. I also tried playing around with the code some more and this works well too,

for item in backend.view():
    lb.insert(END, "{}".format(item))

It's identical to the code Bryan suggested, except instead of join, I used format.

Anyways, thank you so much! This was super helpful.

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