【问题标题】:Command output parsing命令输出解析
【发布时间】:2013-03-29 17:09:19
【问题描述】:

OS X 命令system_profiler SPAudioDataType 给出附录 1 中所示的输出

这在我看来很难解析。

例如:

  • 获取Speaker 的Connection

grep 对 ^\s+Connection: (.*)$ 没有帮助:捕获组 \1 将具有 Connection 值,但也会为嵌套在 Speaker 下的行不获取值。 p>

有趣的是,使用sed 解析可能更容易。例如等待查看音频行,然后是扬声器行,...,然后是 : 后面的文本的正则表达式。

我可以grepMULTILINE 对整个文本进行匹配,首先匹配Speaker,然后匹配Connection,跳过换行符/空格。

是否有一个库可以根据不同缩进的文本行构建嵌套节点的对象模型?

有没有一种类似于 CSS 选择器的方法来查询该对象模型?例如Audio > Speaker > Connection > Value

我喜欢 YAML 考虑空格的方式,但这不会被解析为 YAML。

Java 或 Python 库非常适合学习。

我自己编写了一些代码,然后我决定问:


 def main():
     c = 0
     with open(sys.argv[1]) as f:
         for l in (l.rstrip() for l in f):
             m = l.lstrip()
             if not m:
                 continue
             i = len(l) - len(m)
             if i < c:
               pass # Towards parent
             elif i > c:
               pass # A child
             else:
               pass # A sibling
             c = i

我怀疑我需要假设第一个节点将在缩进 0 并记住所有 seen 缩进级别,以便能够重新附加 减少 缩进比较的节点与其父级的上一级或作为更高级别嵌套的兄弟级。

附录 1


Audio:

    Intel High Definition Audio:

      Audio ID: 29

        Headphone:

          Connection: Combination Output

        Speaker:

          Connection: Internal

        Line Input:

          Connection: Combination Input

        Internal Microphone:

          Connection: Internal

        S/PDIF Optical Digital Audio Input:

          Connection: Combination Input

        S/PDIF Optical Digital Audio Output:

          Connection: Combination Output

        External Microphone / iPhone Headset:

          Connection: Combination Output

        HDMI / DisplayPort Output:

          Connection: Display

    Devices:

        Built-in Microphone:

          Default Input Device: Yes
          Input Channels: 2
          Manufacturer: Apple Inc.
          Current SampleRate: 44100
          Transport: Built-in

        Built-in Input:

          Input Channels: 2
          Manufacturer: Apple Inc.
          Current SampleRate: 44100
          Transport: Built-in

        Built-in Output:

          Default Output Device: Yes
          Default System Output Device: Yes
          Manufacturer: Apple Inc.
          Output Channels: 2
          Current SampleRate: 44100
          Transport: Built-in

【问题讨论】:

  • 这不是有效的 YAML 文档吗?
  • 似乎不是:Python import yaml; yaml.load(open(f).read()) 给 ScannerError: mapping values are not allowed here "耳机:"
  • 这是一个很好的观点 - I 应该如何解析该行? Audio ID: 是独立的项目,持有 29,还是更多项目的子标题?删除29,我认为它会解析。
  • 不起作用。看看我在 Python 中的尝试......你能用创建从缩进级别嵌套的对象模型的东西来完成它吗?

标签: java python text-parsing


【解决方案1】:

这仍然是一个“OSX 管理员”问题。使用-xml flag:

system_profiler -xml SPAudioDataType

输出一个准备解析的 pList 文件。

【讨论】:

  • 这是解决手头问题的方法。完全同意你的看法。但是一般如何解析这种类型的缩进文本呢?
  • @Robottinosino:这根本不是一般的文本格式——它是system_profiler 的设计者可读的特定格式。当然,除非那实际上是一个有效的 yaml 文档
  • 同意,这确实不寻常。有没有一种聪明的方法来构建一个考虑嵌套(嵌套关系由缩进给出)的对象模型,它被编码到一个现成的库中?
  • 你能看到我在 Python 中的尝试吗?我在“Towards parent”中遇到了一些想法。注意:我是一个初学者,真的很想得到一些帮助..
【解决方案2】:

我为解析编写了这个代码:

import sys
import asciitree
class Node(object):
    def __init__(self, name, indent, parent):
        self.name = name
        self.indent = indent
        self.parent = parent
        self.children = []
    def add(self, child):
        self.children.append(child)
    def __str__(self):
        return self.name
def main():
    current_indent = -1
    root_node = Node('Root', current_indent, None)
    current_node = root_node
    with open(sys.argv[1]) as file_to_parse:
        for scanned_line in (l.rstrip() for l in file_to_parse):
            line_content = scanned_line.lstrip()
            if not line_content:
                continue
            indent = len(scanned_line) - len(line_content)
            while True:
              if indent > current_node.indent:
                  parent = current_node
                  break
              elif indent == current_node.indent:
                  parent = current_node.parent
                  break
              else:
                  current_node = current_node.parent
            child = Node(line_content, indent, parent)
            parent.add(child)
            current_node = child
    print(asciitree.draw_tree(root_node))

if __name__ == '__main__':
    main()

产生这个对象模型:

Root
  +--Audio:
     +--Intel High Definition Audio:
     |  +--Audio ID: 29
     |     +--Headphone:
     |     |  +--Connection: Combination Output
     |     +--Speaker:
     |     |  +--Connection: Internal
     |     +--Line Input:
     |     |  +--Connection: Combination Input
     |     +--Internal Microphone:
     |     |  +--Connection: Internal
     |     +--S/PDIF Optical Digital Audio Input:
     |     |  +--Connection: Combination Input
     |     +--S/PDIF Optical Digital Audio Output:
     |     |  +--Connection: Combination Output
     |     +--External Microphone / iPhone Headset:
     |     |  +--Connection: Combination Output
     |     +--HDMI / DisplayPort Output:
     |        +--Connection: Display
     +--Devices:
        +--Built-in Microphone:
        |  +--Default Input Device: Yes
        |  +--Input Channels: 2
        |  +--Manufacturer: Apple Inc.
        |  +--Current SampleRate: 44100
        |  +--Transport: Built-in
        +--Built-in Input:
        |  +--Input Channels: 2
        |  +--Manufacturer: Apple Inc.
        |  +--Current SampleRate: 44100
        |  +--Transport: Built-in
        +--Built-in Output:
        |  +--Default Output Device: Yes
        |  +--Default System Output Device: Yes
        |  +--Manufacturer: Apple Inc.
        |  +--Output Channels: 2
        |  +--Current SampleRate: 44100
        |  +--Transport: Built-in
        +--After Effects 11.0:
        |  +--Manufacturer: Apple, Inc.
        |  +--Current SampleRate: 0
        |  +--Transport: Unknown
        +--Prelude 1.0:
        |  +--Manufacturer: Apple, Inc.
        |  +--Current SampleRate: 0
        |  +--Transport: Unknown
        +--Premiere Pro 6.0:
           +--Manufacturer: Apple, Inc.
           +--Current SampleRate: 0
           +--Transport: Unknown

我现在需要实现像 CSS 选择器这样的搜索功能..

你怎么看?不正确?

【讨论】:

    猜你喜欢
    • 2018-10-25
    • 2014-01-14
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多