Python:格式化程序将数组写入txt文件的方式

时间:2015-11-11 13:37:24

标签: 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],COLOR_FORMAT)+format(clothesAmount[i],AMOUNT_FORMAT))
    outFile.close()

1 个答案:

答案 0 :(得分:0)

更改此行:

outFile.write(format(clothesItem[i],ITEM_FORMAT)+format(clothesColor[i],COLOR_FORMAT)+format(clothesAmount[i],AMOUNT_FORMAT))

以下内容:

outFile.write(format(clothesItem[i], ITEM_FORMAT) + format(clothesColor[i],COLOR_FORMAT) + format(clothesAmount[i], AMOUNT_FORMAT) + "\n")
                                                                                                                                     ^^^^

请注意+ "\n"添加到最后。