Alessandro Peca
Alessandro Peca

Reputation: 923

Bad formatting using random.sample

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?

Upvotes: 1

Views: 89

Answers (4)

Billy
Billy

Reputation: 5609

You're writing the raw list rather than writing each item of the list individually. This will also be forward compatible with Python 3:

with open('textfile.dat') as in_f, open('newfile.dat', 'w+') as out_f:
    out_f.writelines(random.sample(in_f.readlines(), 100))

Upvotes: 0

Stefan Pochmann
Stefan Pochmann

Reputation: 28606

Um... the counterpart of readlines is... drum roll... writelines.

out.writelines(lines)

Upvotes: 3

Luis Alves
Luis Alves

Reputation: 194

You need to write each line at a time.

for line in lines:
    print >> out, l

Upvotes: 0

Błotosmętek
Błotosmętek

Reputation: 12927

You're writing out lines, which is a list, so it is printed using list representation - with brackets around and commas between items. What you want requires printing items in a loop, one by one:

for l in lines:
    print >>out, l

or better (because compatible with Python3):

for l in lines:
    out.write(l)

Oh, and don't forget to out.close()

Upvotes: 1

Related Questions