import random
with open('datafile.txt', 'rb') as f:
    data = f.read().split('\n')
random.shuffle(data)
train_data = data[:50]
test_data = data[50:]
How to split data into trainset and testset randomly?