Reputation: 157
Apologies for my poor programming as i am a terrible programmer. I reference: How to access next line in a file(.txt) in Python
In the question instance the next()
module is able to read the next line.
However, once the code returns to the for loop, it will not be "the next line" but the line after that.
Input:
a /n
b /n
c /n
d /n
Output:
This line contains a, next line contains b.
This line contains c next line contains d.
is it possible to achieve a result such as
This line contains a, next line contains b
This line contains b next line contains c
edit- sorry forgot to input code-sample code:
a = open('file.txt','rb')
for i in a:
b = next(a)
print "this line contains %s, next line contains %s" %(a,b)
a.close()
Upvotes: 2
Views: 9132
Reputation: 3646
You can use the readlines
method of a file object to make a list of a file's lines.
Since each line has a newline character at the end, you'll want to remove that if you wish to print it on a single line that's followed by some other text. You can do this with the strip
method of string objects which removes the trailing character of a string.
When iterating over the lines, you can access the next line in the list by increasing the index by 1
.
And I find it preferable to use the with open(...) as file_name:
syntax when creating file objects as it automatically closes the file for you.
with open('file.txt', 'rb') as text_file:
lines = [line.strip() for line in text_file.readlines()]
for index, line in enumerate(lines):
# Check for last line
try:
next_line = lines[index + 1]
except IndexError:
print("this line contains %s, and is the end of file." % line)
else:
print("this line contains %s, next line contains %s" % (line, next_line))
Upvotes: 4
Reputation: 2086
This would do it I think. However you should think about changing the order of the output. In general you want to first output the current line and then the previous since this is the order in which the file is read. Therefore the solution might be a little confusing. Give an upvote if you like it :-)
a = open('text.txt','rb')
current_line = " "
previous_line = " "
while (previous_line != ""):
# In the first step, we continue and do nothing to read the next line so
# we have both lines.
if current_line == " " and previous_line == " ":
# Get the first line.
current_line = a.readline()
continue
# Update the lines.
previous_line = current_line
current_line = a.readline()
# No output if the next line does not exist.
if current_line == "":
break
# Output.
print "This line contains " + previous_line[0] + ", next line contains " + current_line[0]
a.close()
Upvotes: 1
Reputation: 342
a = open('file.txt','rb')
prev=a.next()
for i in a:
print "this line contains %s, next line contains %s" %(prev,i)
prev=i
a.close()
Upvotes: 2
Reputation: 2385
You could solve your problem by reading all lines into a list of strings.
a = open('file.txt','rb')
text_as_list = a.readlines()
for idx in range(len(text_as_list)-1):
print "this line contains %s, next line contains %s" %(text_as_list[idx], text_as_list[idx+1])
a.close()
Upvotes: 1