1

I want to select every 12th line in a file and write those lines into a new file. Anyone have a suggestion? I have 126 lines, first 6 lines are header, so I need to select the 7th, 19th and 31st line and so on until end of file is reached. and every 10 lines selected should go into a new file.

The way the code is written I can write one file, say P_1 which is made of 10 (every 12th) lines 7,19,31...,109 I want to make 12 files though. So the first file is P_1 which starts at 7th line, P_2 would start at 8th line. How do I loop through to get from 7 to 8 and so on, eventually to line 18?

I would included for i in range to write the new 12 files (will that work?).

for i in range (1,12): with open('output%i.txt' %i,'w+') as g: I just dont know how to get the lines to change so they correspond with the correct file. Ya know what I mean?

Thanks again!

1
  • use the modulo operator %. specifically: (line-6) % 12 == 1
    – Elazar
    May 14, 2013 at 1:43

4 Answers 4

6

If you have a large file, this approach is good as it doesn't load the whole file into memory. (as for line in f.readlines() would)

from itertools import islice #used to get the 7th, 19th, etc... lines
import fileinput #to iterate over lines without loading whole file into memoru

with open('output.txt','w+') as g:
    for line in islice(fileinput.input('file.txt'),6,None,12): #start at 6 because iterable 0-indexed, that is the 7th element has index 6
        g.write(line)

OR (Method pointed out by @Elazar)

with open('output.txt', 'w') as g, open('file.txt') as f:
    for line in islice(f,6,None,12):
        g.write(line)
4
  • Can we just use with open('file.txt') as f and pass f to islice directly here? The whole file won't be loaded into memory at once, right? After all we are using f as an iterator. Pls correct me if I'm wrong.
    – satoru
    May 14, 2013 at 2:04
  • @Satoru.Logic That's true: docs.python.org/2/library/stdtypes.html#file.next, the main reason for using fileinput is so we don't need to have another with open(... to load file.txt.
    – HennyH
    May 14, 2013 at 2:11
  • 1
    For Python 2.7+ you can use a single with to open both files. See @Elazar's answer May 14, 2013 at 2:32
  • @gnibbler I never knew this (I've added that option to my answer)
    – HennyH
    May 14, 2013 at 2:44
3
with open("input") as f:
    for i, line in enumerate(f):
        if (i-7) % 12 == 0: print line.rstrip("\n")
0
1
with open('newfile.txt', 'w') as outfile, open('input.txt') as infile:
    newfile.writelines(k for i, k in enumerate(infile) if i%12==7)
2
  • Quick responses! thanks guys, I will test this out tomorrow when I am in front of python. I will give you some updates. Not sure what Elazar means by "real world". It is a script that I will need to use over and over again, it isn't for school or something. May 14, 2013 at 3:24
  • @user2379814 If so, it is a "real world" script. I used an open() outside with in the first version of this answer. Now this answer is OK, but @HennyH gave a better one.
    – Elazar
    May 14, 2013 at 3:30
0
# creating the file as a base for me, you don't need this part
with open('dan.txt','w') as f:
    f.write('\n'.join(str(i) for i in xrange(1,127)))



# your desired code
with open('dan.txt') as g:
    li = g.readlines()

for i in xrange(6,19):
    with open('P_%d.txt' % (i-5),'w') as f:
        f.writelines(li[x] for x in xrange(i,126,12))
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.