我有一个大文本文件,我想将其拆分为几个不同的较小文本文件。也许有人有代码吗?
Original file:
111
222
333
444
555
666
然后将其拆分为3个txt文件
File 1
111
222
File 2
333
444
File 3
555
666
答案 0 :(得分:1)
如果您想将原始文件拆分为3个文件,而不拆分行,并将文件分成file_01,file_02和file_03,请尝试以下操作:
split --numeric-suffixes=1 -n l/3 original_file file_
答案 1 :(得分:1)
使用GNU awk:
awk 'NR%2!=0{print >"File " ++c}; NR%2==0{print >"File " c}' original_file
或更短:
awk 'NR%2!=0{++c} {print >"File " c}' file
答案 2 :(得分:0)
编辑:问题最初要求提供pythonic解决方案。
整个网站都有类似的问题,但这是你的例子的解决方案:
# read ('r') the file ('text.txt'), and split at each line break ('\n')
textFile = open('text.txt','r').read().split('\n')
# setup temporary array as a place holder for the files (stored as strings) to write,
# and a counter (i) as a pointer
temp = ['']
i = 0
# for each index and element in textfile
for ind,element in enumerate(textFile):
# add the element to the placeholder
temp[i] += element+'\n'
# if the index is odd, and we are not at the end of the text file,
# create a new string for the next file
if ind%2 and ind<len(textFile)-1:
temp.append('')
i += 1
# go through each index and string of the temporary array
for ind,string in enumerate(temp):
# write as a .txt file, named 'output'+the index of the array (output0, output1, etc.
with open('output'+str(ind)+'.txt','w') as output:
output.write(string)