如何从 python 漂亮表中删除重复行?
How to remove duplicating rows from python prettytable?
我使用以下代码从 mysql 数据库输入数据并将其显示在 python shell 中。我使用 prettytable 来使它看起来漂亮。但是现在我遇到了这个错误,如果我在单个 运行 中多次调用显示函数,那么 prettytable 中的行会不断重复自身。请帮助我!!!
import mysql.connector
from prettytable import PrettyTable
x = PrettyTable()
x.field_names = ["Name","AccNo","PhoneNumber","Deposit","Gmail"]
def displayAll():
mycursor.execute("select * from accountsxyz")
for i in mycursor:
x.add_row(i)
print(x)
if ch == '5':
displayAll()
当我运行上面的程序时,如果我多次调用 displayAll 函数,我会得到重复的行!以下是上述 code__
的输出
OUTPUT:::::----
#calling the displayALL function 1st time ::
'''To go back to main menu,press any key+enter (or) just enter:
MAIN MENU
1. NEW ACCOUNT
2. DEPOSIT AMOUNT
3. WITHDRAW AMOUNT
4. BALANCE ENQUIRY
5. ALL ACCOUNT HOLDER LIST
6. CLOSE AN ACCOUNT
7. MODIFY AN ACCOUNT
8. EXIT
Select Your Option (1-8)
5
+-------+-------+-------------+---------+---------------+
| Name | AccNo | PhoneNumber | Deposit | Gmail |
+-------+-------+-------------+---------+---------------+
| AYUSH | 1 | 123456 | 1234 | AQS@GMAIL.COM |
+-------+-------+-------------+---------+---------------+
#calling the displayALL function 2nd time in the same run ::
To go back to main menu,press any key+enter (or) just enter:
MAIN MENU
1. NEW ACCOUNT
2. DEPOSIT AMOUNT
3. WITHDRAW AMOUNT
4. BALANCE ENQUIRY
5. ALL ACCOUNT HOLDER LIST
6. CLOSE AN ACCOUNT
7. MODIFY AN ACCOUNT
8. EXIT
Select Your Option (1-8)
5
+-------+-------+-------------+---------+---------------+
| Name | AccNo | PhoneNumber | Deposit | Gmail |
+-------+-------+-------------+---------+---------------+
| AYUSH | 1 | 123456 | 1234 | AQS@GMAIL.COM |
| AYUSH | 1 | 123456 | 1234 | AQS@GMAIL.COM |
+-------+-------+-------------+---------+---------------+
To go back to main menu,press any key+enter (or) just enter:5
MAIN MENU
1. NEW ACCOUNT
2. DEPOSIT AMOUNT
3. WITHDRAW AMOUNT
4. BALANCE ENQUIRY
5. ALL ACCOUNT HOLDER LIST
6. CLOSE AN ACCOUNT
7. MODIFY AN ACCOUNT
8. EXIT
Select Your Option (1-8)
5
+-------+-------+-------------+---------+---------------+
| Name | AccNo | PhoneNumber | Deposit | Gmail |
+-------+-------+-------------+---------+---------------+
| AYUSH | 1 | 123456 | 1234 | AQS@GMAIL.COM |
| AYUSH | 1 | 123456 | 1234 | AQS@GMAIL.COM |
| AYUSH | 1 | 123456 | 1234 | AQS@GMAIL.COM |
+-------+-------+-------------+---------+---------------+
To go back to main menu,press any key+enter (or) just enter:'''
一个简单的解决方法是在 displayAll
:
中创建 table
def displayAll():
x = PrettyTable()
x.field_names = ["Name","AccNo","PhoneNumber","Deposit","Gmail"]
mycursor.execute("select * from accountsxyz")
for i in mycursor:
x.add_row(i)
print(x)
编辑:更简单的是:
from prettytable import from_db_cursor
def displayAll():
mycursor.execute("select * from accountsxyz")
print(from_db_cursor(mycursor))
我使用以下代码从 mysql 数据库输入数据并将其显示在 python shell 中。我使用 prettytable 来使它看起来漂亮。但是现在我遇到了这个错误,如果我在单个 运行 中多次调用显示函数,那么 prettytable 中的行会不断重复自身。请帮助我!!!
import mysql.connector
from prettytable import PrettyTable
x = PrettyTable()
x.field_names = ["Name","AccNo","PhoneNumber","Deposit","Gmail"]
def displayAll():
mycursor.execute("select * from accountsxyz")
for i in mycursor:
x.add_row(i)
print(x)
if ch == '5':
displayAll()
当我运行上面的程序时,如果我多次调用 displayAll 函数,我会得到重复的行!以下是上述 code__
的输出OUTPUT:::::----
#calling the displayALL function 1st time ::
'''To go back to main menu,press any key+enter (or) just enter:
MAIN MENU
1. NEW ACCOUNT
2. DEPOSIT AMOUNT
3. WITHDRAW AMOUNT
4. BALANCE ENQUIRY
5. ALL ACCOUNT HOLDER LIST
6. CLOSE AN ACCOUNT
7. MODIFY AN ACCOUNT
8. EXIT
Select Your Option (1-8)
5
+-------+-------+-------------+---------+---------------+
| Name | AccNo | PhoneNumber | Deposit | Gmail |
+-------+-------+-------------+---------+---------------+
| AYUSH | 1 | 123456 | 1234 | AQS@GMAIL.COM |
+-------+-------+-------------+---------+---------------+
#calling the displayALL function 2nd time in the same run ::
To go back to main menu,press any key+enter (or) just enter:
MAIN MENU
1. NEW ACCOUNT
2. DEPOSIT AMOUNT
3. WITHDRAW AMOUNT
4. BALANCE ENQUIRY
5. ALL ACCOUNT HOLDER LIST
6. CLOSE AN ACCOUNT
7. MODIFY AN ACCOUNT
8. EXIT
Select Your Option (1-8)
5
+-------+-------+-------------+---------+---------------+
| Name | AccNo | PhoneNumber | Deposit | Gmail |
+-------+-------+-------------+---------+---------------+
| AYUSH | 1 | 123456 | 1234 | AQS@GMAIL.COM |
| AYUSH | 1 | 123456 | 1234 | AQS@GMAIL.COM |
+-------+-------+-------------+---------+---------------+
To go back to main menu,press any key+enter (or) just enter:5
MAIN MENU
1. NEW ACCOUNT
2. DEPOSIT AMOUNT
3. WITHDRAW AMOUNT
4. BALANCE ENQUIRY
5. ALL ACCOUNT HOLDER LIST
6. CLOSE AN ACCOUNT
7. MODIFY AN ACCOUNT
8. EXIT
Select Your Option (1-8)
5
+-------+-------+-------------+---------+---------------+
| Name | AccNo | PhoneNumber | Deposit | Gmail |
+-------+-------+-------------+---------+---------------+
| AYUSH | 1 | 123456 | 1234 | AQS@GMAIL.COM |
| AYUSH | 1 | 123456 | 1234 | AQS@GMAIL.COM |
| AYUSH | 1 | 123456 | 1234 | AQS@GMAIL.COM |
+-------+-------+-------------+---------+---------------+
To go back to main menu,press any key+enter (or) just enter:'''
一个简单的解决方法是在 displayAll
:
def displayAll():
x = PrettyTable()
x.field_names = ["Name","AccNo","PhoneNumber","Deposit","Gmail"]
mycursor.execute("select * from accountsxyz")
for i in mycursor:
x.add_row(i)
print(x)
编辑:更简单的是:
from prettytable import from_db_cursor
def displayAll():
mycursor.execute("select * from accountsxyz")
print(from_db_cursor(mycursor))