CustomerList = []
CustomerList.append (c1)
CustomerList.append (c2)
CustomerList.append (c3)
CustomerList.append (c4)
CustomerList.append (c5)
for c in CustomerList:
print ("%8s %25s %35s %15s %15s" % (c.name, c.birth, c.address, c.hkid, "$"+str(c.balance)+"HKD"))
我上面存储了c1-5值,因此我将结果显示为
Jack Jan, 10th, 1996 430 Davis Ct., San Francisco M8875895 $40000HKD
Smith March 24th, 1997 3-5 Tai Koo Shing, Hong Kong M3133242 $600HKD
Suzy May 5th, 1995 32 Clearwater Bay Ave. Hong Kong M8378644 $100000HKD
我是这样的。
我想使用文件访问权限将此信息存储到文本文件中。
我想要做的是使用
inputFile = open("Customer.txt","w")
inputFile.write (....)
我不知道我应该如何列入......空白。
答案 0 :(得分:1)
with open("Customer.txt","w") as f: #closes file automtically after loop ends
for c in CustomerList:
f.write("%8s %25s %35s %15s %15s \n" % (c.name, c.birth, c.address, c.hkid, "$"+str(c.balance)+"HKD"))
答案 1 :(得分:0)
用write替换print语句,然后写一个换行符,然后关闭文件
inputFile = open("Customer.txt","w")
for c in CustomerList:
inputFile.write("%8s %25s %35s %15s %15s" % (c.name, c.birth, c.address, c.hkid, "$"+str(c.balance)+"HKD"))
inputFile.write('\n')
inputFile.close()