如何在for循环的输出中添加新行

时间:2018-10-01 01:47:46

标签: python-3.x for-loop

嗨,我试图理解这一点:

因此,此“ file.txt”的输出如下:我如何使其看起来像这样:

现在看起来像:eatdogcatrat

想: 吃 狗 ..

基本上需要使每个内容都移到下一行。

my_locations = open("file.txt" , "w")
for mylist in channel:
    print(mylist)
    my_locations.write(mylist)
my_locations.close()

1 个答案:

答案 0 :(得分:0)

希望这会起作用!

channel = ['eat', 'dog', 'cat', 'rat']

my_locations = open("file.txt", "w")
for mylist in channel:
    print(mylist)
    my_locations.write(mylist + '\n') # to have each go to the next line.
my_locations.close()