Let's say the text file is this:
1
2
3
4
5
6
...
What I want is to randomely order the contents in groups of N lines, without shuffling the lines in each group, like this:
#In this case, N = 2.
5
6
1
2
7
8
...
My file isn't huge, it will be less than 50 lines for sure.
I've tried to do this using this code:
import random
with open("data.txt", "r") as file:
lines = []
groups = []
for line in file:
lines.append(line[:-1])
if len(lines) > 3:
groups.append(lines)
lines = []
random.shuffle(groups)
with open("data.txt", "w") as file:
file.write("\n".join(groups))
But I get this error:
Traceback (most recent call last):
File "C:/PYTHON/python/Data.py", line 19, in <module>
file.write("\n".join(groups))
TypeError: sequence item 0: expected str instance, list found
Is there a simpler way of doing this?