Python 打印列表时,数据未对齐

Python 打印列表时,数据未对齐,python,list,Python,List,我有一个代码,可以读取一个库存txt文件,该文件在运行时会为用户显示一个菜单。但是,当它运行时,数量和pice列未对齐: Select an item ID to purchase or return: ID Item Quantity Price 244 Large Cake Pan 7.00 19.99 576 Assorted Sprinkles 3.00 12.89 212 Deluxe Icing Set 6.00 37.9

我有一个代码,可以读取一个库存txt文件,该文件在运行时会为用户显示一个菜单。但是,当它运行时,数量和pice列未对齐:

Select an item ID to purchase or return: 

ID  Item          Quantity   Price
244 Large Cake Pan     7.00  19.99
576 Assorted Sprinkles     3.00  12.89
212 Deluxe Icing Set       6.00  37.97
827 Yellow Cake Mix    3.00   1.99
194 Cupcake Display Board      2.00  27.99
285 Bakery Boxes       7.00   8.59
736 Mixer      5.00 136.94

Enter another item ID or 0 to stop
这是我的密码:

import InventoryFile
def readFile ():
    #open the file and read the lines
    inventoryFile = open ('Inventory.txt', 'r')
    raw_data = inventoryFile.readlines ()

    #remove the new line characters
    clean_data = []
    for item in raw_data:
        clean_item = item.rstrip ('\n')
        clean_data.append (clean_item)

    #read lists into objects
    all_objects = []
    for i in range (0, len(clean_data), 4):
        ID = clean_data [i]
        item = clean_data [i+1]
        qty = float (clean_data [i+2])
        price = float (clean_data [i+3])

        inventory_object = InventoryFile.Inventory (ID, item, qty, price)

        all_objects.append (inventory_object)

    return all_objects

def printMenu (all_data):
    print ()
    print ('Select an item ID to purchase or return: ')
    print ()
    print ('ID\tItem\t\t  Quantity\t Price')

    for item in all_data:
        print (item)

    print ()

    print ('Enter another item ID or 0 to stop')

def main ():
    all_items = readFile ()
    printMenu (all_items)

main ()
如何格式化输出,以便数量和价格列正确对齐

以下是库存类别:

class Inventory:
    def __init__ (self, new_id, new_name, new_stock, new_price):
        self.__id = new_id
        self.__name = new_name
        self.__stock = new_stock
        self.__price = new_price

    def get_id (self):
        return self.__id
    def get_name (self):
        return self.__name
    def get_stock (self):
        return self.__stock
    def get_price (self):
        return self.__price

    def restock (self, new_stock):
        if new_stock < 0:
            print ('ERROR')
            return False
        else:
            self.__stock = self.__stock + new_stock
            return True

    def purchase (self, purch_qty):
        if (new_stock - purch_qty < 0):
            print ('ERROR')
            return False
        else:
            self.__stock = self.__stock + purch_qty
            return True

    def __str__ (self):
        return self.__id + '\t' + self.__name + '\t' + \
        format (self.__stock, '7.2f') + format (self.__price, '7.2f')
类别清单:
定义初始(自我、新id、新名称、新股票、新价格):
self.\uu id=新的\u id
self.\uuuu name=新名称
自身库存=新库存
自身价格=新价格
def get_id(自身):
返回自我
def get_名称(自身):
返回self.\u名称
def获取库存(自我):
返回自己的库存
def获取价格(自身):
返回自己的价格
def补充库存(自有、新库存):
如果新库存<0:
打印('错误')
返回错误
其他:
自售股票=自售股票+新股票
返回真值
def采购(自购、采购数量):
如果(新库存-采购数量<0):
打印('错误')
返回错误
其他:
自库存=自库存+采购数量
返回真值
定义(自我):
返回self.\u id+'\t'+self.\u name+'\t'+\
格式(self.\u股票,'7.2f')+格式(self.\u价格,'7.2f'))

使用您的类
清单
的getter,您可以创建一个列表并加入输出

def printMenu (all_data):
    print ()
    print ('Select an item ID to purchase or return: ')
    print ()
    print ('ID\tItem\t\t  Quantity\t Price')

    for item in all_data:
        product_id = item.get_id()
        product_name = item.get_name()
        product_stock = item.get_stock()
        product_price = item.get_price()
        output = [product_id, product_name, product_stock, product_price]
        output = [str(item) for item in output]
        print('{:<5}\t{:<5}\t{:<5}\t{:<5}'.format(output))

    print ()

    print ('Enter another item ID or 0 to stop')
def打印菜单(所有数据):
打印()
打印('选择要购买或退货的项目ID:')
打印()
打印('ID\tItem\t\t数量\t价格')
对于所有_数据中的项目:
product\u id=item.get\u id()
产品名称=项目。获取名称()
产品库存=项目。获取库存()
产品价格=项目。获取价格()
输出=[产品id、产品名称、产品库存、产品价格]
输出=[输出中项目的str(项目)]

打印({:Read up about
str.format()
,例如
print({:5}{:20}{:10}{:10})。格式('ID','Item','Quantity','Price'))
的可能副本作为旁白,在Python中,您通常不编写getter和setter,也不使用双下划线名称损坏,即
self.\uu stock
除非您确实想要/需要该行为。如果您想遵循“private”的约定,只需使用
self.stock
self.\u stock
我尝试了这个,但是我得到了'TypeError:join()*之后的参数必须是可编辑的,而不是Inventory'What is
Inventory
?从您的回答中,我假设您使用的是Tuples抱歉,我没有指定,Inventory是一个类。这个新的解决方案应该可以工作,如果它介意选择我的答案并在晚上打电话给它的话?我感谢您投入的时间,但我得到了另一个错误:TypeError:s序列项2:应为str实例,找到浮点