如何从文本文件中提取 select 行,然后随机化这些行的顺序? (在 python 中)

How to select lines from a text file and then randomize the order of those lines? (in python)

我遇到的问题是我想 select 文本文件中一定范围内的多行,然后随机排列这些行的顺序。我知道这是随机化文本文件的方法,但我如何针对一定数量的行进行随机化?

with open("infile.txt") as f:
    lines = f.readlines()
random.shuffle(lines)
with open("outfile.txt", "w") as f:
    f.writelines(lines)```

不确定这是最好的解决方案,但它有效

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)