Python 基础教程

Python 高级教程

Python 相关应用

Python 笔记

python 一行一行即逐行写入 csv、txt 等文件

Python 笔记 Python 笔记


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')