Python 将二维数组(对象)存储到文本文件中
我在上面存储了c1-5值,所以得到的结果如下Python 将二维数组(对象)存储到文本文件中,python,file,file-handling,Python,File,File Handling,我在上面存储了c1-5值,所以得到的结果如下 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.a
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"))
我是这样的
我想使用文件访问将此信息存储到文本文件中
我想做的是使用
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
我不知道我应该如何包括在。。。。空白。用write替换print语句,然后写一个换行符,然后关闭文件
inputFile = open("Customer.txt","w")
inputFile.write (....)
我得到了你告诉我的并应用到我的代码中。我很感激。顺便说一句,我可以为append做同样的事情吗?我有新增加的客户,他们的名字是c6,c7,c8,…是的,你可以用append<代码>输入文件=打开(“Customer.txt”,“a”)
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()
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"))