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)
找了下具体描述: