I have a text file with say, 100 lines, I want to randomly segment these lines into 80-20 lines into two separate text files, Using the code below but it's not doing proper partition. I am getting a different number of files. I should get 80 lines in file2 and 20 files in file1. can someone point out the error and plz suggest if there is a better way. please note in total.txt is the original file which needs to be segmented into file1 and file 2. '''
def partition(l, pred):
fid_train=open('meta/file1.txt','w')
fid_test = open('meta/file2.txt','w')
for e in l:
if pred(e):
fid_test.write(e)
else:
#fid_train.write(e+'\n')
fid_train.write(e)
return fid_train,fid_test
lines = open("meta/total_list.txt").readlines()
lines1, lines2 = partition(lines, lambda x: random.random() < 0.2)
'''
Aucun commentaire:
Enregistrer un commentaire