Ways to read/edit multiple lines in python
Solution 1
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.
Solution 2
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.
Roeben
Updated on February 07, 2020Comments
-
Roeben over 4 years
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 over 10 yearsIsn'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?
-
Totem over 10 yearsI 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 over 10 yearsIf your lists won't be too big though, you are probably fine with f.readlines
-
Roeben over 10 yearsThanks! The file does divide nicely into fours, so that isn't an issue. I'm going to give this a try first.
-
Totem over 10 yearsplease 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 over 10 yearsI 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.