Python:格式化程序将数组写入txt文件的方式
我试图让我的程序将每个数组中的一项打印到文本文件中,然后在写入所有第一项后,将数组的第二项写入第二行,依此类推 我现在的代码只在一行文本上打印信息Python:格式化程序将数组写入txt文件的方式,python,Python,我试图让我的程序将每个数组中的一项打印到文本文件中,然后在写入所有第一项后,将数组的第二项写入第二行,依此类推 我现在的代码只在一行文本上打印信息 def write(): outFile=open("Inventory.txt","w") for i in range(0,len(clothesItem)): outFile.write(format(clothesItem[i],ITEM_FORMAT)+format(clothesColor[i],C
def write():
outFile=open("Inventory.txt","w")
for i in range(0,len(clothesItem)):
outFile.write(format(clothesItem[i],ITEM_FORMAT)+format(clothesColor[i],COLOR_FORMAT)+format(clothesAmount[i],AMOUNT_FORMAT))
outFile.close()
更改此行:
对下列事项:
outFile.write(format(clothesItem[i], ITEM_FORMAT) + format(clothesColor[i],COLOR_FORMAT) + format(clothesAmount[i], AMOUNT_FORMAT) + "\n")
^^^^
请注意末尾添加的
+“\n”
。若要编写多行文本,请使用换行符“\n”。我将把换行符放在哪里。我不能把它放在外文件里
outFile.write(format(clothesItem[i], ITEM_FORMAT) + format(clothesColor[i],COLOR_FORMAT) + format(clothesAmount[i], AMOUNT_FORMAT) + "\n")
^^^^