【问题标题】:Parse text for matching key then grab first set of matching table name rows解析匹配键的文本,然后获取第一组匹配的表名行
【发布时间】:2020-12-28 20:02:08
【问题描述】:

尝试对一些大型的旧平面文本文件(老实说是一团糟)进行一些核对。我遇到的问题是我找到了匹配的键,我试图获取第一组具有匹配表名的连续行,而忽略其余行。我将如何阅读我需要的内容而不是其他内容?玩弄休息,但逻辑在逃避我。
示例:如果我正在寻找 101 的 PK 和饮料的表名,我想从下面的列表中打印

喝 25
喝 26

FlatTextFile.txt
pk_tbl 23 100
食品 0 0
喝 0 0
甜点 0 0

pk_tbl 101
食品 0
喝 25
喝 26
甜点 0
喝 27
喝 28
喝 29

pk_tbl 102
食品 0
喝 0
喝 0
喝 0
甜点 0

我所在的示例的伪代码

        pk_flag = 0
            for row in d:
                if (row[0]= 'drink') and (pk_flag =='1'):
                    print(row)                    
                if (row[0]= 'pk_tbl')and (row[2] =='101'):
                    pk_flag = 1;
                elif (row[0]= 'pk_tbl')and (row[2] !='101'):
                    pk_flag = 0;

有点困惑哈哈,感谢任何帮助。 谢谢!

【问题讨论】:

    标签: python text-parsing


    【解决方案1】:
    def get_table_data(file_path = 'FlatTextFile.txt', table_keyword = 'pk_tbl', table_num = '101', data_keyword = 'drink'):
        output_ls = []
        with open(file_path, 'r') as fh:
            table = False
            data = False
            for line in fh.readlines():
                if not len(line.strip()): # Ignoring blank lines
                    continue
                row = line.split()
                if not table: # Searching for table keyword and number
                    if row[0] == table_keyword and row[1] == table_num:
                        table = True
                else:
                    if row[0] == table_keyword: # I'm already at next table
                        break
                    if not data: # Searching for data keyword
                        if row[0] == data_keyword:
                            data = True
                            output_ls.append(line)
                    else: # Searching for more consecutive data keywords
                        if row[0] == data_keyword:
                            output_ls.append(line)
                        else:
                            break
            return output_ls
    

    【讨论】:

      【解决方案2】:

      假设文件FlatTextFile.txt中的pattern存储为:

      table name
      food # (none or one or more)
      drink # (none or one or more)
      desert # (none or one or more)
      (food, drink, desert pattern can repeat for a table)
      (blank line)
      table name (next table name)
      (food, drink, desert pattern in any order)
      

      您想在找到表pk_tbl 101 后立即选择带有drink 的记录。表名可以是pk_tbl+任意字符串或无字符串+101

      基于上述假设,下面是从表 101 中挑选特定饮品的代码。

      with open ('FlatTextFile.txt', 'r') as f:
          table = False
          output = []
          line_count = 0
          for line in f:
              line = line.rstrip()
              x = line.split()
              if {'pk_tbl','101'} <= set(x): #checks if 'pk_tbl' and '101' are in x
                  table = True
                  continue
              if table and 'drink' in x: #finds values with drinks
                  line_count +=1
                  output.append(line)
                  continue
              if line_count > 0: break #we are past drink in table pk_tbl; stop processing
      print (output)
      

      这个输出将是:

      ['drink 25', 'drink 26']
      

      【讨论】:

        猜你喜欢
        • 2017-11-30
        • 2014-09-16
        • 2017-07-16
        • 1970-01-01
        • 2011-06-27
        相关资源
        最近更新 更多