python 利用两层 with open as ... 语句,可以边一行一行读指定一个文件,处理后逐行写入另一个文件。
推荐解决方法
def process(read_file, write_file, fp_func):
with open(write_file, 'w') as w:
with open(read_file, 'r') as r:
for line in r:
lst = line.split('\t')
label = lst[0]
json_feature = lst[1]
fiv = fp_func(json_feature)
w.writelines(f'{label}\t{fiv}\n')