【问题标题】:Fixed width text file to python dictionary固定宽度的文本文件到 python 字典
【发布时间】:2016-11-18 11:11:52
【问题描述】:

我正在尝试在 Python 中导入一个类似于下面报告的文本文件。

+ CATEGORY_1 first_part of long attribute <NAME_a>
|     ...second part of long attribute
|    + CATEGORY_2: a sequence of attributes that extend over 
|    |     ... possibly many <NAME_b>
|    |     ... lines
|    |    + SOURCE_1 => source_code 
|    + CATEGORY_2: another sequence of attributes that extend over <NAME_c>
|    |     ... possibly many lines
|    |    + CATEGORY_1: yet another sequence of <NAME_d> attributes that extend over
|    |    |     ...many lines 
|    |    |    + CATEGORY_2: I really think <NAME_e> that
|    |    |    |     ... you got the point 
|    |    |    |     ... now
|    |    |    |    + SOURCE_1 => source_code 
|    + SOURCE_2 => path_to_file 

假设我可以轻松识别由 <...>

分隔的对象名称

我的理想输出是反映 txt 文件层次结构的 Python 字典,例如:

{NAME_a : {'category' : CATEGORY_1,
           'depencencies' : {NAME_b : {'category' : CATEGORY_2,
                                       'source_type' : SOURCE_1,
                                       'source_code' : source_code}
                             NAME_c : {'category' : CATEGORY_2,
                                       'dependencies' : { NAME_d : {'category' : CATEGORY_1,
                                                                    'dependencies' : NAME_e : {'category' : CATEGORY_2,
                                                                                               'source_type' : SOURCE_1,
                                                                                               'source_code' : source_code}
                                                                    }
                                                        }           
            'source_type' : SOURCE_2,
            'source_code : path_to_file
           }
}

认为这里的主要思想是在行开始之前计算制表符的数量,这将决定层次结构。 我试图查看 pandas read_fwf 和 numpy loadfromtxt,但没有任何成功。 你能指出解决这个问题的相关模块或策略吗?

【问题讨论】:

  • 任何有关如何解决该问题的提示将不胜感激。不只是寻找“开箱即用”的解决方案。
  • 策略:由于你的数据结构是扁平的(它是一个文本文件),你需要开发自己的解析器来猜测水平,识别名称......要构建字典结构,你需要一个堆栈.

标签: python text io


【解决方案1】:

不是一个完整的答案,但您可以使用堆栈来遵循一种方法。

每次输入类别时,都会将类别键推入堆栈。 然后您阅读该行,检查选项卡的数量并据此存储。如果级别与前一个级别相同或更高,则从堆栈中弹出一个项目。 然后你只需要基本的正则表达式来提取项目。

一些 Python/伪代码,让您有一个想法

levels = []
items = {}
last_level = 0

for line in file:
   current_level = count_tabs()
   if current_level > last_level:
      name = extract_name(line)
      levels.append(name)
      items = fill_dictionary_in_level(name, line)
   else:
      levels.pop()
   last_level = current_level

return items

【讨论】:

    【解决方案2】:

    这是一个策略:

    对于每一行,使用正则表达式解析该行并提取数据。

    这是一个草稿:

    import re
    
    line = "|    + CATEGORY_2: another sequence of attributes that extend over <NAME_c>"
    
    level = line.count("|") + 1
    mo = re.match(r".*\+\s+(?P<category>[^:]+):.*<(?P<name>[^>]+)>", line)
    category = mo.group("category")
    name = mo.group("name")
    
    print("level: {0}".format(level))
    print("category: {0}".format(category))
    print("name: {0}".format(name))
    

    你得到:

    level: 2
    category: CATEGORY_2
    name: NAME_c
    

    【讨论】:

      猜你喜欢
      • 2013-01-01
      • 1970-01-01
      • 2010-12-16
      • 2016-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-22
      相关资源
      最近更新 更多