I need to take a number n
of random lines from a text file, and then write them into a new text file. The code works but the lines are taken and written with a different format than that in the original file, and I want to keep the same. This is the code:
import random
with open('textfile.dat') as f:
lines = random.sample(f.readlines(),100) #n=100 for example
out=open('newfile.dat', 'w+')
print >>out, lines
A small part of textfile.dat:
626.0649 310.7645 122.8257
626.1954 310.5412 123.0418
627.0475 310.7212 123.2242
626.6918 311.0145 123.6525
...
And a small part of newfile.dat:
['632.3587 304.9801 101.2915\n', '644.4067 307.2889 114.6692\n', '626.0014 312.0789 98.2628\n', '653.1418 307.1721 100.6861\n', ...
I'm using python 2.7 on macOS. How can I fix it?
Aucun commentaire:
Enregistrer un commentaire