python 将TXT拆分每个5万行

python 将TXT拆分每个5万行

import time
import hashlib

filtpath="test.txt"

        
with open(filtpath, "r") as f:
    print(time.strftime("%Y-%m-%d %H:%M:%S") + " 开始..." + filtpath)
    line_num = 0
    file_num = 1
    for line in f.readlines():
        line = line.strip("
")  #去掉列表中每一个元素的换行符
        #print(line)
        line_num += 1
        if line_num % 50000 == 0 :
            file_num += 1
            print(file_num)
        with open( filtpath[0:-4]+ "_"+  str(file_num) + ".txt","a") as file_handle:
            file_handle.write(line)
            file_handle.write("
")
    print("line_num=" + str(line_num))
    print(time.strftime("%Y-%m-%d %H:%M:%S") + " 结束..." + filtpath)
hmoban主题是根据ripro二开的主题,极致后台体验,无插件,集成会员系统
自学咖网 » python 将TXT拆分每个5万行