How to select lines from a text file and then randomize the order of those lines? (in python)
The problem I have is that I want to select a number of lines within a range from a text file, and then randomize the order of those lines. I know that this is how to randomize the text file, but how do I target a certain number of lines to randomize?
with open("infile.txt") as f:
lines = f.readlines()
random.shuffle(lines)
with open("outfile.txt", "w") as f:
f.writelines(lines)```
Not sure that is the best solution, but it works
n_start=1
n_end=4
with open("infile.txt") as f:
lines = f.readlines()
tmp=lines[n_start:n_end]
random.shuffle(tmp)
lines[n_start:n_end]=tmp
with open("outfile.txt", "w") as f:
f.writelines(lines)