Python写入时连续添加内容而不是覆盖原来内容

今天处理数据,即把a.txt中内容与Link.csv中内容匹配,有就输出Link.csv的一整行。

类似于字符串与子串的相匹配问题,这里写入c.txt时发现数据不断被覆盖了,源代码如下:

with open('a.txt') as f_a:
    for line_a in f_a:
        with open('Link.csv') as f_b:
            for line_b in f_b:
                if line_a in line_b:
                    with open('c.txt', 'w') as f_c:
                        f_c.write(line_b)

经过搜查与测试,发现我们以“a”方式打开文档即可

with open('a.txt') as f_a:
    for line_a in f_a:
        with open('Link.csv') as f_b:
            for line_b in f_b:
                if line_a in line_b:
                    with open('c.txt', 'a') as f_c:
                        f_c.write(line_b)

找了下具体描述:

image.png

posted @ 2022-05-23 12:32  我在吃大西瓜呢  阅读(275)  评论(0编辑  收藏  举报