【问题标题】:Getting data in between two strings in Python2.7在Python2.7中的两个字符串之间获取数据
【发布时间】:2014-12-01 16:33:00
【问题描述】:

我正在尝试从包含如下数据的文本文件中获取两个字符串之间的数据:

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之前的数据应该进入Navigated pages 1,命令2和1之间的数据应该进入Navigated pages 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)

我不需要commands 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

任何指导将不胜感激

【问题讨论】:

    标签: python string python-2.7 function-parameter


    【解决方案1】:

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

    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
    

    【讨论】:

    • 感谢您的回复。我再次编辑了我的问题,以使其更易于理解。这里的问题是这个函数在另一个执行循环的函数中使用。我在一个函数调用中只需要一个Navigated pages ,而不是所有导航页面。例如,第一个函数调用我需要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,依此类推..
    • 每次调用都会一次又一次的读取文件,可能负载很重。如果您解析一次文件并将导航标记存储在列表中,那不是更好吗?
    猜你喜欢
    • 2012-12-28
    • 2020-01-09
    • 1970-01-01
    • 2011-10-12
    • 1970-01-01
    • 1970-01-01
    • 2012-02-11
    相关资源
    最近更新 更多