我正在尝试使用记录和列表以及在不同客户之间的这种简单的收据代码,我希望有一个新行,但是因为它在列表\n
中不起作用。
我尝试将\n
添加到代码中的各个部分,并尝试添加print("\n")
,但这也不起作用。
from collections import *
customer_details = namedtuple("Customer","ID First_Name Surname Age Gender Product Price")
cus1 = customer_details(16785, "John","Apleased",36,"Male","coffee",70)
customers = [cus1]
cus2 = customer_details(10, "Steve","Jobs",67,"male","tea",40)
customers.append(cus2)
print(customers)
当您查看列表时,客户之间应该有间隔。
答案 0 :(得分:1)
您可以使用for
循环
>>> for customer in customers:
... print(customer)
...
Customer(ID=16785, First_Name='John', Surname='Apleased', Age=36, Gender='Male', Product='coffee', Price=70)
Customer(ID=10, First_Name='Steve', Surname='Jobs', Age=67, Gender='male', Product='tea', Price=40)
或者您可以使用'\n'.join()
,但是首先需要将customers
从namedtuple
的列表转换为字符串列表
>>> print('\n'.join(str(customer) for customer in customers))
Customer(ID=16785, First_Name='John', Surname='Apleased', Age=36, Gender='Male', Product='coffee', Price=70)
Customer(ID=10, First_Name='Steve', Surname='Jobs', Age=67, Gender='male', Product='tea', Price=40)
答案 1 :(得分:0)
您可以使用join方法在新行中打印每个元素
print("\n".join(customers))