加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

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

发布时间:2020-12-16 22:49:17 所属栏目:Python 来源:网络整理
导读:我是Python编程的新手,使用Python 3.x,并且正在使用Barbershop P.O.S系统,管理员将有权添加服务及其相应的价格. 我正在使用Pretty Table库来实现打印带有serviceID,服务和价格的表格. 这是我的代码: from prettytable import PrettyTableimport randomservi

我是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 listn")
     services.append(prompt1)

     prompt2 = input("Please enter a price for the servicen")
     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 listn")
...     try:
...         #- Type Casting.
...         prompt2 = int(raw_input("Please enter a price for the servicen"))
...     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()

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读