简体   繁体   English

如何从python中的特定行读取?

[英]How can I read from specific lines in python?

I have an app in python where I'm reading from a plain text. 我在python中有一个应用,可以从纯文本中读取内容。

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 : 例如,这是我的纯文本文件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" ? 我想从“ part1”中获取color1和color2,所以我要逐行阅读,但是如果我更改color1的位置为color2,则会出现错误,是否有办法读取“ 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. 如您所见,我按行获取内容,我想读取“ 1”部分中的所有内容,我尝试不使用任何行,但是这样做时,它将读取“ 2部分”或第一个“ color1和color2”他们发现。

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或color2,如果我改变位置,程序应该读取它,而在第2部分中也应如此。

#!/usr/lib/env python
import re

file = open("color.txt","r")
content = file.read()
file.close()
content = content.split('PART ')[1:]
dic = {}
for part in content:
    dic[int(part[0])] = part[1:]

def color(color_index, part_index):
    color = re.search('color{_color_index}\s(.*?)\s'.format(_color_index=color_index),dic[part_index]).group(1)
    print 'color',color_index,'of PART',part_index,":",color
    return color

color(1,1)#color 1 of PART 1 : gray
color(2,1)#color 2 of PART 1 : blue
color(1,2)#color 1 of PART 2 : yellow
color(2,2)#color 2 of PART 2 : green

This way, you can get what you want no matter how parts and colors are arranged. 这样,无论零件和颜色如何布置,都可以得到想要的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM