[英]How do I select specific lines from a file based on line number?
I have a class which takes in the name of the module to be tested. 我有一个采用要测试的模块名称的类。 Then the class scans though a master text file and finds the files that need to be compiled based on the module selected. 然后,该类将扫描一个主文本文件,并根据所选模块找到需要编译的文件。
This is what I have so far: 这是我到目前为止的内容:
class Module():
def __init__ (self,name):
self.name = name
module_start_line = []
with open('master_module_list.txt', 'r') as master_list:
for start_no, master_lines in enumerate(master_list):
if re.search('module ' + self.name, master_lines):
module_start_line.append(start_no)
print(module_start_line)
module eeprom_top
../../../project_ip/eeprom_ip/src/eeprom_pkg.vhd
../../../project_ip/eeprom_ip/src/eeprom_top.vhd
module fan_ip
../../../project_ip/fan_ip/src/fan_pkg.vhd
../../../project_ip/fan_ip/src/fan_pkg.vhd
module backplane
../../../project_ip/backplane_ip/src/backplane.vhd
So if the user types in "fan_ip" then I want to copy the two lines after module fan_ip
from the text file. 因此,如果用户键入“ fan_ip”,那么我想从文本文件中复制module fan_ip
之后的两行。
I am fairly new to python so feel free to correct me on my previous code. 我对python相当陌生,因此可以随时根据我之前的代码对我进行更正。 I am using python 3.4. 我正在使用python 3.4。
您可以使用readline
方法一次读取一行,并记录所读取的内容以确定每一行的行号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.