有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java命令输出解析

OSX命令system_profiler SPAudioDataType给出了附录1中所示的输出

对我来说,这看起来很难解析

例如:

  • 获取SpeakerConnection

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

有趣的是,用sed解析可能更容易。e、 g.等待查看线路音频,然后是线路扬声器。。。,然后是:后面文本的regex

我可以grepMULTILINE在全文中,首先匹配Speaker,然后Connection,跳过换行符/空格

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

有没有一种方法可以查询类似于CSS选择器的对象模型?e、 g.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,并记住所有看到的缩进级别,以便能够将一个缩进比前一级别减少的节点重新连接到其父节点,或作为嵌套级别更高的同级节点

附录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


共 (2) 个答案

  1. # 1 楼答案

    这仍然是一个“OSX管理员”问题。使用^{} flag

    system_profiler -xml SPAudioDataType
    

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

  2. # 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选择器这样的搜索功能

    你觉得呢?不对