本次代码借鉴网上高手,放在这里以备个人之需
#读入用于切割的原始大文件 open_diff = open(r'G:\LOST.DIR\all.txt', 'r',encoding = 'UTF-8') diff_line = open_diff.readlines() line_list = [] for line in diff_line: line_list.append(line) #切分diff,2000的意思是每个单个小文件保存2000lines diff_match_split = [line_list[i:i+2000] for i in range(0,len(line_list),2000)] #将切分的写入多个txt中\n", for i,j in zip(range(0,len(diff_match_split)),range(0,len(diff_match_split))): with open('newdiff_match%d.txt'% j,'w+') as temp: #with open('newdiff_match%d.txt'% j,'w+',encoding = "utf-8") as temp:通过encoding可以自定义想要的编码格式,不设置encoding,默认保存为GBK即ANSI的格式 for line in diff_match_split [i]: temp.write(line)
加油鸭!^_^