如何从python的输出中删除大括号? [英] How to remove curly bracket from the output in python?
问题描述
我是 Python 新手,我最近在 1 周前开始学习,但我被困在这里...任何帮助将不胜感激...
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
推荐答案
您正在将列表或元组而不是字符串插入到列表框中.在传递给插入方法之前,您需要将每一行显式转换为字符串.否则,底层 tcl 解释器将尝试通过在将值转换为字符串时添加花括号或反斜杠来保留数据的类似列表的结构.
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.
考虑这个代码:
query = ('''SELECT * FROM Employee;''' )
cur.execute(query)
row = cur.fetchall()
row
一个列表列表.对于 row
中的每个项目,您都有该行中每一列的值列表.
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)
如前所述,x
是每列的数据列表.但是, listbox.insert
需要一个字符串.由于它不是字符串,tkinter 将使用其自己的逻辑将其转换为字符串,这可能会导致添加花括号.
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.
为避免这种情况,您应该在调用 insert
之前将列表显式转换为字符串.最简单的方法是在每个元素之间添加一个空格:
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))
如果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))
这篇关于如何从python的输出中删除大括号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!