I have an app in python where I'm reading from a plain text.
It's working fine. My question is there is a possible way to read from multiple lines instead of line by line. For example here's is my plain text file color.txt:
###
#####
#########
#example colors
#line of colors
#line colors PART 1
color1 gray
color2 blue
# line colors PART 2
iface eth1 inet static
color1 yellow
color2 green
I want color1 and color2 from "part1" so I'm reading this line by line but if I change position of color1 for color2 I'm getting an error, so is there a way to read everything inside of "part1" ? that way I can get the same result.
Here's my full code:
from flask import Flask,render_template,flash,request,redirect
import os
import sys
app = Flask(__name__)
def color1_p1():
with open('color.txt', 'r+') as f:
for i, line in enumerate(f):
if i == 7:
found_color = line.find('color1')
if found_color != -1:
color = line[found_color+len('color1:'):]
print ('Color: '), color
return color
def color2_p1():
with open('color.txt', 'r+') as f:
for i, line in enumerate(f):
if i == 8:
found_color = line.find('color2')
if found_color != -1:
color = line[found_color+len('color2:'):]
print ('Color: '), color
return color
def color1_p2():
with open('color.txt', 'r+') as f:
for i, line in enumerate(f):
if i == 13:
found_color = line.find('color1')
if found_color != -1:
color = line[found_color+len('color1:'):]
print ('Color: '), color
return color
def color2_p2():
with open('color.txt', 'r+') as f:
for i, line in enumerate(f):
if i == 14:
found_color = line.find('color2')
if found_color != -1:
color = line[found_color+len('color2:'):]
print ('Color: '), color
return color
@app.route('/')
def showLine():
color1 = color1_p1()
color2 = color2_p1()
color3 =color1_p2()
color4 = color2_p2()
return render_template('color.html', color1=color1, color2=color2, color3=color3,color4=color4)
if __name__ == '__main__':
app.run(debug=True)
as you can see I'm getting the content by lines, I want to read everything inside of part "1" , I tried without the lines but when doing so it will read the "part 2" or the first "color1 and color2" they find.
Here's my output:
All I want is to read color1 or color2 no matter the line it is, if I change the position the program should read this, and same should happen in part 2.

color1