dimanche 23 août 2015

Opening, randomizing, writing a list of lines in Python 2.7.6.1

I want to take a large list of vocabulary words (each word + definition is a line), randomize the list, and then write it a file in 142 item blocks without repeating any words.

I think I'm roughly close, but the writing lines back is giving me trouble. Here's what I have:

import random
zz = 0
with open('vocablist.text') as f:
    lines = f.readlines()
    lines = [line.rstrip('\n') for line in open('vocablist.txt')]
random.shuffle(lines)
vocab_out = open('vocab_out.txt.', 'w')
for x in lines:
    zz = zz +1
    if zz > 142:
        vocab_out.write('END OF SECTION'+\n)
        zz = 0
    vocab_out.write(x)
vocab_out.close()

I have to re-make these vocab lists for a class I took over and doing it by hand would take me days.




Aucun commentaire:

Enregistrer un commentaire