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

前端之家收集整理的这篇文章主要介绍了如何在Python中使用Pretty Table打印多个列表中的数据?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我是Python编程的新手,使用Python 3.x,并且正在使用Barbershop P.O.S系统,管理员将有权添加服务及其相应的价格.
我正在使用Pretty Table库来实现打印带有serviceID,服务和价格的表格.

这是我的代码

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

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

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

当我输入第二个服务和价格时,输出是这样的:

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

我想输出是这样的:

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

有谁知道如何实现这一目标?
任何帮助,将不胜感激.

最佳答案
我们可以通过add_row方法将行添加到PrettyTable对象中.

演示:

>>> from prettytable import PrettyTable
>>> import random
>>> 
>>> x = PrettyTable(["ServiceID","Service","Price"])
>>> 
>>> while True:
...     #- Get value
...     ID = random.randint(1,90000) #range high to lower probability of non-uniqueness
...     prompt1 = raw_input("Please add a service name to the list\n")
...     try:
...         #- Type Casting.
...         prompt2 = int(raw_input("Please enter a price for the service\n"))
...     except ValueError:
...         print("Please enter valid type")
...         continue
...     #- Add row
...     x.add_row([ID,prompt1,prompt2])
...     #- Ask user to Continue or not.
...     choice = raw_input("Continue yes/ no:").lower()
...     if not(choice=="yes" or choice=="y"):
...         break
... 
Please add a service name to the list
2
Please enter a price for the service
3
Continue yes/ no:y
Please add a service name to the list
4
Please enter a price for the service
6
Continue yes/ no:y
Please add a service name to the list
5
Please enter a price for the service
7
Continue yes/ no:n
>>> print x
+-----------+---------+-------+
| ServiceID | Service | Price |
+-----------+---------+-------+
|   38515   |    2    |   3   |
|    8680   |    4    |   6   |
|   51188   |    5    |   7   |
+-----------+---------+-------+
>>> 

删除行:

使用del_row()方法.我们需要传递要删除的行的索引.

>>> print x
+-----------+---------+-------+
| ServiceID | Service | Price |
+-----------+---------+-------+
|   38515   |    2    |   3   |
|    8680   |    4    |   6   |
|   51188   |    5    |   7   |
+-----------+---------+-------+
>>> x.del_row(1)
>>> print x
+-----------+---------+-------+
| ServiceID | Service | Price |
+-----------+---------+-------+
|   38515   |    2    |   3   |
|   51188   |    5    |   7   |
+-----------+---------+-------+

如果我们提供的索引值大于异常中的总行数,则需要处理异常,需要在ur代码中处理.

例外情况是:

>>> x.del_row(10)
Traceback (most recent call last):
  File "

注意:

>对Python 2.x使用raw_input()
>对Python 3.x使用input()

猜你在找的Python相关文章