To get an arbitrary number of lines from your file, you can do something like the following:
import random with open('file.txt') as f: lines = random.sample(f.readlines(),5)
In the above example, 5 rows are returned, but you can easily change that number you need. You can also change it to randint() to get a random number of lines in addition to a number of random lines, but you need to make sure that the sample size is no larger than the number of lines in the file. Depending on your input, this can be trivial or a bit more complicated.
Note that lines can appear in lines in a different order in which they appear in the file.
Dave webb
source share