Python:从循环中获取所有结果,而不仅仅是最后一个?

Python:从循环中获取所有结果,而不仅仅是最后一个?,python,for-loop,Python,For Loop,我正在用python运行一个循环,我希望每次的结果都打印到一个txt文件中。 但是通过运行我的代码,只打印循环最后一次的结果。 我怎样才能得到全部结果 这是我的密码: for tr_list in tw_table.findAll('tr'): for p_list in tr_list.findAll('td')[-1].findAll('p'): pt=p_list.get_text().encode('utf-8') for tag_list in tr_l

我正在用python运行一个循环,我希望每次的结果都打印到一个txt文件中。 但是通过运行我的代码,只打印循环最后一次的结果。 我怎样才能得到全部结果

这是我的密码:

for tr_list in tw_table.findAll('tr'):
    for p_list in tr_list.findAll('td')[-1].findAll('p'):
        pt=p_list.get_text().encode('utf-8')
    for tag_list in tr_list.findAll('td')[0].findAll('p'):
        tagt=tag_list.get_text().encode('utf-8')
        result = tagt,pt
        result = str(result)
        f = open("output.txt","w")
        f.write(result)
        f.write('\n')
        print result
output.txt应该是多行,如:

result:
123
456
789
但事实是output.txt只有最后一行:

789

在非附加模式下打开文件时,其所有内容都会被清除。因为在循环中打开它,所以除了最后一个结果之外,所有内容都会丢失

    f = open("output.txt","w")
    f.write(result)
    f.write('\n')
你应该打开这个文件一次。完成循环并在完成后关闭

f = open("output.txt","w")
for tr_list in tw_table.findAll('tr'):
    for p_list in tr_list.findAll('td')[-1].findAll('p'):
        pt=p_list.get_text().encode('utf-8')
    for tag_list in tr_list.findAll('td')[0].findAll('p'):
        tagt=tag_list.get_text().encode('utf-8')
        result = tagt,pt
        result = str(result)
        f.write(result)
        f.write('\n')
f.close()

在非附加模式下打开文件时,其所有内容都会被清除。因为在循环中打开它,所以除了最后一个结果之外,所有内容都会丢失

    f = open("output.txt","w")
    f.write(result)
    f.write('\n')
你应该打开这个文件一次。完成循环并在完成后关闭

f = open("output.txt","w")
for tr_list in tw_table.findAll('tr'):
    for p_list in tr_list.findAll('td')[-1].findAll('p'):
        pt=p_list.get_text().encode('utf-8')
    for tag_list in tr_list.findAll('td')[0].findAll('p'):
        tagt=tag_list.get_text().encode('utf-8')
        result = tagt,pt
        result = str(result)
        f.write(result)
        f.write('\n')
f.close()
打开文件一次,并在迭代时保持文件句柄打开:

with open("output.txt", "w") as f:
    for tr_list in tw_table.findAll('tr'):
        for p_list in tr_list.findAll('td')[-1].findAll('p'):
            pt = p_list.get_text().encode('utf-8')
        for tag_list in tr_list.findAll('td')[0].findAll('p'):
            tagt = tag_list.get_text().encode('utf-8')
            result = tagt, pt
            result = str(result)
            f.write(result)
            f.write('\n')
            print result
打开文件一次,并在迭代时保持文件句柄打开:

with open("output.txt", "w") as f:
    for tr_list in tw_table.findAll('tr'):
        for p_list in tr_list.findAll('td')[-1].findAll('p'):
            pt = p_list.get_text().encode('utf-8')
        for tag_list in tr_list.findAll('td')[0].findAll('p'):
            tagt = tag_list.get_text().encode('utf-8')
            result = tagt, pt
            result = str(result)
            f.write(result)
            f.write('\n')
            print result

f=open(“output.txt”,“w”)中的
“w”
更改为
“a”
,将
“w”
更改为
f=open(“output.txt”,“w”)中的
“a”
,是!你说得对,谢谢!你的答案最简单是的!你说得对,谢谢!你的答案是最简单的。谢谢你的回答!对于我的问题,你的答案是正确的!这是最好的答案,因为1)文件只打开一次。重复打开/关闭文件对性能不利。2) 因为它使用带有(open(…)的
作为f:
,所以即使for循环中出现异常,文件也始终正确关闭。@dano是的,您的评论非常正确,在其他情况下,这个答案是最合理的!谢谢你的回答!对于我的问题,你的答案是正确的!这是最好的答案,因为1)文件只打开一次。重复打开/关闭文件对性能不利。2) 因为它使用带有(open(…)的
作为f:
,所以即使for循环中出现异常,文件也始终正确关闭。@dano是的,您的评论非常正确,在其他情况下,这个答案是最合理的!嘿,欢迎来到StackOverflow。请记住向上投票有用的答案和解决问题的答案,将此问题标记为已解决。嘿,欢迎来到StackOverflow。请记住将有用的答案和解决问题的答案向上投票,以将此问题标记为已解决。