有勇气的牛排博客

Python按行分割大文件txt/res等

有勇气的牛排 464 Python 2023-05-18 21:03:37

在用单脚本处理大文件时,可能会遇到各种问题,故需要将文件切分为小文件来操作

# 每 50 0000 行拆成一个文件 wdata = [] count = 0 with open('E:/xxx/xxx/xxx.txt', 'r', encoding='utf8') as f: d = f.readline() while d: if count%500000==0: print(count) with open("file_" + str(count) + ".res", 'w+',encoding='utf-8') as f_target: for mydata in wdata: f_target.write(mydata) wdata = [] count+=1 wdata.append(d) d = f.readline()

参考地址:
https://blog.csdn.net/GrandpaTong/article/details/108346941


留言

专栏
文章
加入群聊