在Python2.7中获取两个字符串之间的数据

在Python2.7中获取两个字符串之间的数据,python,string,python-2.7,function-parameter,Python,String,Python 2.7,Function Parameter,我试图从包含以下数据的文本文件中获取两个字符串之间的数据: X_0_Gui_Homescreen_FPS_List commands 1 :SensorFps ( 30.000) X_0_Gui_Homescreen_Homescreen X_0_Gui_Homescreen_EI_Switchview X_0_Gui_Homescreen_EI_Set commands 2 :SetExIndex (12), SetExIndex (13), EiSwitchAssign (1, 13) X_

我试图从包含以下数据的文本文件中获取两个字符串之间的数据:

X_0_Gui_Homescreen_FPS_List
commands 1 :SensorFps ( 30.000)
X_0_Gui_Homescreen_Homescreen
X_0_Gui_Homescreen_EI_Switchview
X_0_Gui_Homescreen_EI_Set
commands 2 :SetExIndex (12), SetExIndex (13), EiSwitchAssign (1, 13)
X_0_Gui_Homescreen_EI_Switchview
X_0_Gui_Homescreen_Homescreen
X_0_Gui_Menu_000_Menu_root
X_0_Gui_Menu_100_Menu_Recording
commands 3 :MediaCodec (4), SetSensorFormat (0)
X_0_Gui_Menu_110_Menu_Recording_Project
commands 4 :ProjectFps (0), SensorFps ( 23.976)
X_0_Gui_Menu_100_Menu_Recording
X_0_Gui_Menu_000_Menu_root
X_0_Gui_Menu_300_Menu_Outputs
X_0_Gui_Menu_310_Menu_Outputs_EVF_mon
commands 5 :ZoomPos (0)
X_0_Gui_Menu_312_Menu_Outputs_EVF_exptools
commands 6 :ExposureToolSel (0, 1), ExposureToolSel (1, 1), ZebraMode (0, 0), ZebraMode (1, 0)
X_0_Gui_Menu_310_Menu_Outputs_EVF_mon
X_0_Gui_Menu_317_Menu_Outputs_EVF_settings
X_0_Gui_Menu_310_Menu_Outputs_EVF_mon
X_0_Gui_Menu_311_Menu_Outputs_EVF_overlays
commands 7 :CenterMark (0, 1)
我希望得到如下输出:

Navigated pages 1:
                  X_0_Gui_Homescreen_FPS_List
Navigated pages 2:
                  X_0_Gui_Homescreen_Homescreen
                     X_0_Gui_Homescreen_EI_Switchview
                        X_0_Gui_Homescreen_EI_Set
Navigated pages 3:
                  X_0_Gui_Homescreen_EI_Switchview
                     X_0_Gui_Homescreen_Homescreen
                        X_0_Gui_Menu_000_Menu_root
                           X_0_Gui_Menu_100_Menu_Recording
等等。。。(命令1之前的数据应进入
导航页面1
,命令2和1之间的数据应进入
导航页面2
,依此类推)

迄今为止完成的代码:

def get_navigated_path(command_number):
    navigated_and_commands = open('navigated_and_commands','r')
    data = navigated_and_commands.read()
    block = ""
    for i in range(0,command_number):
        block = re.compile(ur'commands ' + str(i) + ' :' + '[\S ]+\s((?:(?![^\n]+commands ' + str(i+1) + ' :' + ').)*)', re.IGNORECASE | re.DOTALL)
    data_with_block=re.findall(block, data)
    for line in data_with_block:
        if "X_" in line:
            print "       > navigated pages: \n" + "                         " + line
它几乎可以产生所需的输出,并且

       > navigated pages: 
                         X_0_Gui_Homescreen_Homescreen
X_0_Gui_Homescreen_EI_Switchview
X_0_Gui_Homescreen_EI_Set
commands 2 :SetExIndex (12), SetExIndex (13), EiSwitchAssign (1, 13)
我不需要行
命令2:SetExIndex(12)、SetExIndex(13)、EiSwitchAssign(1,13)
我也希望它像这样组织起来

   > navigated pages: 
                     X_0_Gui_Homescreen_Homescreen
                        X_0_Gui_Homescreen_EI_Switchview
                           X_0_Gui_Homescreen_EI_Set

非常感谢您的指导

我不会使用正则表达式。。。可能是一个简单的迭代,可以适合以下情况:

def get_navigated_path(command_number):
    navigated_and_commands = open('navigated_and_commands','r')
    i = 0
    for line in navigated_and_commands.readlines():
        if line.startswith("commands %d"%command_number):
            break
        if line.startswith("commands"):
            i += 1
            print "Navigated pages %d"%i
            continue
        print line

谢谢你的回复。我再次编辑了我的问题,以便更好地理解。这里的问题是这个函数在另一个执行循环的函数中使用。在一次函数调用中,我只需要一个
导航页面
,而不是所有导航页面。例如,第一次调用我需要
导航页面1:X_0_Gui_Homescreen\u FPS_List
,第二次调用我需要
导航页面2:X_0_Gui_Homescreen\u Homescreen\u Gui_Homescreen\u EI_Switchview X_0_Gui_Homescreen\u EI_Set
等等call将反复读取文件,这可能会导致负载过重。如果只解析一次文件并将导航标记存储在列表中,不是更好吗?