如何在Python中使用Pretty Table从多个列表中打印数据?

2024-05-20 09:32:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我对Python编程比较陌生,使用Python 3.x,正在开发一个Barbershop p p.O.S系统,管理员有权添加服务和相应的价格。 我使用漂亮的表库来实现打印出一个带有serviceID、service和price的表。

这是我的代码:

from prettytable import PrettyTable
import random

serviceID = []
services = []
price = []
x = PrettyTable()

x.add_column("ServiceID",[serviceID])
x.add_column("Service", [services])
x.add_column("Price", [price])

while True:
try:

     ID = random.randint(1,90000) #range high to lower probability of non-uniqueness
     serviceID.append(ID) #Generates unique ID for each service
     prompt1 = input("Please add a service name to the list\n")
     services.append(prompt1)

     prompt2 = input("Please enter a price for the service\n")
     prompt2 == int(prompt2)
     price.append(prompt2)

     print(x)


except ValueError:
    print("Please enter valid type")
    continue

当我输入第一个服务和价格时,输出为:

+-----------+---------+--------+
| ServiceID | Service | Price  |
+-----------+---------+--------+
|   [9880]  | ['box'] | ['90'] |
+-----------+---------+--------+

当我输入第二个服务和价格时,输出如下:

+---------------+-----------------+--------------+
|   ServiceID   |     Service     |    Price     |
+---------------+-----------------+--------------+
| [9880, 47612] | ['box', 'trim'] | ['90', '80'] |
+---------------+-----------------+--------------+

我希望输出如下:

+---------------+-----------------+--------------+
|   ServiceID   |     Service     |    Price     |
+---------------+-----------------+--------------+
|  9880         |      box        |       90     |
|  47612        |     trim        |       80     |
+---------------+-----------------+--------------+

有人知道如何做到这一点吗? 任何帮助都将不胜感激。


Tags: importboxaddidservicecolumn价格random
1条回答
网友
1楼 · 发布于 2024-05-20 09:32:10

我总是在while循环中创建类prettytable.PrettyTable的新实例,从而实现了这一点。

from prettytable import PrettyTable
import random

serviceID = []
services = []
price = []

while True:
    try:
        x = PrettyTable()

        ID = random.randint(1,90000) #range high to lower probability of non-uniqueness
        serviceID.append(ID) #Generates unique ID for each service

        prompt1 = input("Please add a service name to the list\n")
        services.append(prompt1)

        prompt2 = input("Please enter a price for the service\n")
        prompt2 == int(prompt2)
        price.append(prompt2)

        x.add_column("ServiceID", serviceID)
        x.add_column("Service", services)
        x.add_column("Price", price)

        print(x)


    except ValueError:
        print("Please enter valid type")
        continue

这是我使用方法field_names()add_row()编写的代码版本。

from prettytable import PrettyTable
import random

serviceID = []
services = []
price = []

x = PrettyTable()

x.field_names = ["ServiceID", "Service", "Price"]

while True:
    try:
         ID = random.randint(1,90000) #range high to lower probability of non-uniqueness
         serviceID.append(ID) # in order to store new value if you will need it later

         prompt1 = input("Please add a service name to the list\n")
         services.append(prompt1) # in order to store new value if you will need it later

         prompt2 = input("Please enter a price for the service\n")
         prompt2 == int(prompt2)
         services.append(prompt2) # in order to store new value if you will need it later

         x.add_row([ID, prompt1, prompt2])
         print(x)

    except ValueError:
        print("Please enter valid type")
        continue

相关问题 更多 >