1.打开文件的时候通过 newline=‘\n’。带命参数指定结尾的换行符
2.打开文件时通过这种方式的优点:with 语句句柄负责打开和关闭文件(包括在内部块中引发异常时),for line in f 将文件对象 f 视为一个可迭代的数据类型,会自动使用 IO 缓存和内存管理,这样就不必担心大文件了
write_file_path1 = r"D:\crumdata\tsaleformal\collect-events20191101-EventAppId25.json"
read_file_path = r"D:\crumdata\tsaleformal\collect-events20191101.json"
match_rule = ',"$EventAppId":25}\n'
wf1 = open(write_file_path1, 'w', encoding = 'utf-8',newline='\n')
with open(read_file_path, 'r', encoding = 'utf-8') as f:
for line in f:
line = line.replace('}\n',match_rule)
wf1.write(line)
wf1.close()
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)