您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

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

5b51 2022/1/14 8:21:59 python 字数 6850 阅读 506 来源 www.jb51.cc/python

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

概述

我是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     |
+---------------+-----------------+--------------+

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

演示:

>>> 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 "
  
   prettytable.py",line 832,in del_row
    raise Exception("Cant delete row at index %d,table only has %d rows!" % (row_index,len(self._rows)))
Exception: Cant delete row at index 10,table only has 2 rows!

  

注意:

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

总结

以上是编程之家为你收集整理的如何在Python中使用Pretty Table打印多个列表中的数据?全部内容,希望文章能够帮你解决如何在Python中使用Pretty Table打印多个列表中的数据?所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶