Python 2.7 - Split comma separated text file into smaller text files -


i (unsuccessfully) trying figure out how create list of compound letters using loops. beginner programmer, have been learning python few months. fortunately, later found solution problem - genearte list of strings compound of letters other list in python - see first answer.

so took code , added little needs. randomized list, turned list comma separated file. code:

from string import ascii_lowercase al itertools import product import random  list = ["".join(p) in xrange(1,6) p in product(al, repeat = i)]  random.shuffle(list)  joined = ",".join(list)  f = open("double_letter_generator_output.txt", 'w')  print >> f, joined f.close() 

what need split massive file "double_letter_generator_output.txt" smaller files. each file needs consist of 200 'words'. need split many files. files of course not exist yet , need created program also. how can that?

here's how it, i'm not sure why you're splitting smaller files. @ once, i'm assuming file big stored in working memory, i'm traversing 1 character @ time.

let bigfile.txt contain

1,2,3,4,5,6,7,8,9,10,11,12,13,14

max_num_elems = 2 #you'll want 200 namecounter = 1 numelemscounter = 0 open('bigfile.txt', 'r') bigfile:     outputfile = open('output' + str(namecounter) + '.txt', 'a')     letter in bigfile.read():         if letter == ',':             numelemscounter += 1         if numelemscounter == max_num_elems:             numelemscounter = 0             outputfile.close()             namecounter += 1             outputfile = open('output' + str(namecounter) + '.txt', 'a')         else:             outputfile.write(letter);     outputfile.close() 

now output1.txt 1,2, output2.txt 3,4, output3.txt 5,6, etc.

$ cat output7.txt 13,14 

this little sloppy, should write nice function , format way like!

fyi, if want write bunch of different files, there's no reason write 1 big file first. write little files right off bat.

this way, last file might have fewer max_num_elems elements.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -