4

What i'm trying to do is to take 4 lines from a file that look like this:

@blablabla
blablabla #this string needs to match the amount of characters in line 4
!blablabla
blablabla #there is a string here

This goes on for a few hundred times.

I read the entire thing line by line, make a change to the fourth line, then want to match the second line's character count to the amount in the fourth line.

I can't figure out how to "backtrack" and change the second line after making changes to the fourth.

with fileC as inputA:
    for line1 in inputA:
        line2 = next(inputA)
        line3 = next(inputA)
        line4 = next(inputA)

is what i'm currently using, because it lets me handle 4 lines at the same time, but there has to be a better way as causes all sorts of problems when writing away the file. What could I use as an alternative?

Roeben
  • 67
  • 1
  • 1
  • 5

3 Answers3

5

you could do:

with open(filec , 'r') as f:
    lines = f.readlines() # readlines creates a list of the lines

to access line 4 and do something with it you would access:

lines[3] # as lines is a list

and for line 2

lines[1] # etc.

You could then write your lines back into a file if you wish

EDIT:

Regarding your comment, perhaps something like this:

def change_lines(fileC):

    with open(fileC , 'r') as f:
        while True:
            lines = []
            for i in range(4):
                try:
                    lines.append(f.next()) # f.next() returns next line in file
                except StopIteration: # this will happen if you reach end of file before finding 4 more lines. 
                    #decide what you want to do here
                    return
            # otherwise this will happen
            lines[2] = lines[4] # or whatever you want to do here
            # maybe write them to a new file
            # remember you're still within the for loop here

EDIT:

Since your file divides into fours evenly, this works:

def change_lines(fileC):
    with open(fileC , 'r') as f:
        while True:
            lines = []
            for i in range(4):
                try:
                    lines.append(f.next())
                except StopIteration:
                    return
            code code # do something with lines here
                      # and write to new file etc.
Totem
  • 7,189
  • 5
  • 39
  • 66
  • Isn't it generally a bad idea to put all of your lines in a single list? What if the input file is very large? Is it possible to only load the first four lines, do the edits, then grab the next 4 lines? What would this code look like? – Roeben Feb 03 '14 at 15:59
  • I edited my post. You can do that with something like what I added. It works for me using f.next() anyway, though you may need to tinker with it. Catching the StopIteration is only necessary if you have a file that doesn't evenly divide into 4. So for instance, on the last iteration of the for loop you only have 2 or 3 lines left in the file etc. So you'll need to figure out what to do in that situation. – Totem Feb 03 '14 at 17:48
  • If your lists won't be too big though, you are probably fine with f.readlines – Totem Feb 03 '14 at 17:50
  • Thanks! The file does divide nicely into fours, so that isn't an issue. I'm going to give this a try first. – Roeben Feb 03 '14 at 19:03
  • please let me know if this worked for you. And if you found this post solved your issue, please consider accepting it as the answer. – Totem Feb 03 '14 at 23:49
  • I made some changes to the code which I think make a bit more sense. At the bottom where it says 'code code' I unindented that line. – Totem Feb 06 '14 at 13:37
5

Another way to do it:

import sys
from itertools import islice

def read_in_chunks(file_path, n):
    with open(file_path) as fh:
        while True:
            lines = list(islice(fh, n))
            if lines: yield lines
            else:     break

for lines in read_in_chunks(sys.argv[1], 4):
    print lines

Also relevant is the grouper() recipe in the itertools module. In that case, you would need to filter out the None values before yielding them to the caller.

FMc
  • 41,963
  • 13
  • 79
  • 132
0

You could read the file with .readlines and then index which ever line you want to change and write that back to the file:

rf = open('/path/to/file')
file_lines = rf.readlines()
rf.close()

line[1] = line[3] # trim/edit however you'd like

wf = open('/path/to/write', 'w')
wf.writelines(file_lines)
wf.close()
tijko
  • 7,599
  • 11
  • 44
  • 64