Reputation: 185
I have a text file which contains the following:
NUM,123
FRUIT
DRINK
FOOD,BACON
CAR
NUM,456
FRUIT
DRINK
FOOD,BURGER
CAR
NUM,789
FRUIT
DRINK
FOOD,SAUSAGE
CAR
I'm trying to change BURGER
but how to do that?
file = open('input.txt', 'r')
while True:
line = file.readline()
if '456' in line:
print line
break
With the above code, I want to pinpoint it using the distinct number after NUM
, but I am only able to read the line where 456
occurs. How to read 3 lines below 456
and then access the BURGER
part?
Thanks!
Update using Levon's solution:
with open('input.txt','r') as f:
data = f.readlines()
for i, line in enumerate(data):
if '456' in line:
field = ','.join(data[i+3].split(',')[1])
field = field.replace(field,'PIZZA')
Now how do you write everything back into a new file?
Upvotes: 0
Views: 4149
Reputation: 143032
Update based on comment below:
Use this code if all of the file can fit into memory (reads all of file at once):
with open("data.txt") as f:
data = f.readlines()
for i, line in enumerate(data):
if "456" in line:
print data[i+3].split(',')[1],
We read the whole file into a list (data
), and then use enumerate() to access both the index (i
), and the element at the given index (ie. the line
). Since we have all of the data in the list, once we find the line with "456" we can look for the line at i + 3
very easily in our list and use split() to separate out the word we are interested in.
--
Use this code if file is potentially very big (processes file line-by-line):
with open("data.txt") as f:
count = -1
for line in f:
if "456" in line:
count = 4
count -= 1
if count == 0:
print line.split(',')[1],
will print "BURGER"
This looks for the 2nd word, 3 lines after the string "456". It assumes the words are separated by a comma (,
)
Note that using the with
construct also takes care of closing the file when we are done (or if we encounter an exception).
Upvotes: 2
Reputation: 2323
Your text file has structure. I presume you are really asking for a way to read that structure, for example so that someone is able to change their order to a pizza.
Your input.txt has some orders. Each order starts with an order number (NUM,456 etc.), then some items, then CAR
as if to show that the person in drive through has finished with their order.
So, step 1.
# changes order 456's food to a pizza, from whatever it was
file = open('input.txt', 'r')
bad_order = 'NUM,%d' % 456
new_food = 'FOOD,%s' % 'Pizza'
change = False
for line in file:
line = line.strip()
msg = ''
if line.startswith('NUM,'):
msg = 'Ignoring order: '
if line == bad_order:
msg = 'Changing order: '
change = True
if change and line.startswith('FOOD,'):
line = new_food
if line == 'CAR':
change = False
print '%s%s' % (msg, line)
Further steps might be to recognize the structure more formally (eg add orders to a collection for processing later); or refactor this into a function so you can test changing different order numbers and different food types, not just FOOD
.
EDIT
I've edited the code to make it clear how it currently reads your file. The output is now:
Ignoring order: NUM,123 FRUIT DRINK FOOD,BACON CAR Changing order: NUM,456 FRUIT DRINK FOOD,Pizza CAR Ignoring order: NUM,789 FRUIT DRINK FOOD,SAUSAGE CAR
Upvotes: 0
Reputation: 10937
Try:
file = open('input.txt', 'r')
for line in file.readlines():
if '456' in line:
stored_line = line
if 'BURGER' in line:
print stored_line
This will give you the correct line in the file and you will have access to the number preceding it. To edit the file in place, look to a solution like this.
If you are just interested in the text stream. You can you add line.replace('BURGER', 'FOO')
after the print.
Upvotes: 0