我有一个如下代码,如果在file1中读取的行符合我的条件,我想编辑/更新我的file1。另外,我想编辑满足file2中另一个条件的行:
with gzip.open('/my/file1.txt.gz', 'r') as f:
for line in csv.reader(f, delimiter="\t"):
if (str(line[3]) == "C"):
# edit/update the line from file 1
else:
with gzip.open('/my/file2.txt.gz', 'r') as f2:
for line2 in csv.reader(f2, delimiter="\t"):
if line2[0] == line[0]:
# edit/update the line2 from file 2
有没有办法实现这一目标?提前谢谢!
答案 0 :(得分:0)
这一行中有些东西:
keys = set()
with gzip.open('/my/file1.txt.gz', 'r') as src_file:
for line in csv.reader(src_file, delimiter="\t"):
if (str(line[3]) == "C"):
# edit/update the line from file 1
else:
keys.add(line[0])
with gzip.open('/my/file2.txt.gz', 'r') as target_file:
for line in csv.reader(target_file, delimiter="\t"):
if line[0] in keys:
答案 1 :(得分:0)
一些问题:
您正在尝试更新只读文件;
您似乎想要在阅读过程中更新文件。对于纯文本文件,只要替换字符串的长度与替换字符串的长度完全相同,就可以完成此操作。 但是这是一个压缩文件 - 我认为你会破坏文件。
每次要修改时,都会重新读取第二个文件。
如果可能的话,我建议将整个作品读入记忆,进行修改,并将其全部写回:
import csv, gzip
from collections import defaultdict
def read_gzip(fname):
with gzip.open(fname, 'rb') as inf:
incsv = csv.reader(inf, delimiter='\t')
return list(incsv)
def write_gzip(fname, data):
with gzip.open(fname, 'wb') as outf:
outcsv = csv.writer(outf, delimiter='\t')
outcsv.writerows(data)
def main():
f1, f1_dirty = read_gzip('/my/file1.txt.gz'), False
f2, f2_dirty = read_gzip('/my/file2.txt.gz'), False
# make row index on f2
idx = defaultdict(list)
for row in f2:
idx[row[0]].append(row)
# process files
for row in f1:
if row[3] == 'C':
# modify row data
f1_dirty = True
else:
for other in idx[row[0]]:
# modify other
f2_dirty = True
if f1_dirty: write_gzip('/my/file1.txt.gz', f1)
if f2_dirty: write_gzip('/my/file2.txt.gz', f2)
if __name__=="__main__":
main()