【问题标题】:Convert tabbed text into Perl data format将选项卡式文本转换为 Perl 数据格式
【发布时间】:2012-07-20 06:25:08
【问题描述】:

我有一些来自 VLM telnet 服务的数据:

show
    media : ( 1 broadcast - 0 vod )
        cam1
            type : broadcast
            enabled : yes
            loop : no
            inputs
                1 : rtsp://xxx:xxx@xxx.xxx.xxx.xxx:xxx/xxxx/xxx.xxx
            output : #transcode{vcodec="h264"}:standard{access=http,mux=ts,dst=xxx.xxx.xxx.xxx:6690/cam1}
            options
            instances
                instance
                    name : default
                    state : playing
                    position : 0,000000
                    time : 0
                    length : -1
                    rate : 1,000000
                    title : 0
                    chapter : 0
                    can-seek : 0
                    playlistindex : 1
    schedule

有没有办法将此数据转换为 XML 或 JSON 或其他 Perl 支持的格式(哈希表等)?

【问题讨论】:

    标签: xml json perl hashtable vlc


    【解决方案1】:

    这些数据非常接近 YAML,也许是故意的。你需要做的就是

    • 添加一个初始行---来标记内容的开始

    • 删除所有 cmets,如 ( 1 broadcast - 0 vod )

    • 在所有当前不包含冒号的行中添加一个尾随冒号

    除了media 节点不能既等于评论又等于cam1 节点的容器之外,现有的评论就可以了。

    此程序编辑数据以形成正确的 YAML,将其加载到 Perl 哈希中并转储结果。

    use strict;
    use warnings;
    
    use YAML 'Load';
    
    open my $fh, '<', 'VLM.txt' or die $!;
    
    my $yaml = "---\n";
    
    while (<$fh>) {
      s/\s*\(.*//;
      s/$/ :/ unless /:/;
      $yaml .= $_;
    }
    
    my $data = Load($yaml);
    
    use Data::Dump;
    dd $data;
    

    输出

    {
      show => {
        media => {
          cam1 => {
            enabled   => "yes",
            inputs    => { 1 => "rtsp://xxx:xxx\@xxx.xxx.xxx.xxx:xxx/xxxx/xxx.xxx" },
            instances => {
                           instance => {
                             "can-seek"      => 0,
                             "chapter"       => 0,
                             "length"        => -1,
                             "name"          => "default",
                             "playlistindex" => 1,
                             "position"      => "0,000000",
                             "rate"          => "1,000000",
                             "state"         => "playing",
                             "time"          => 0,
                             "title"         => 0,
                           },
                         },
            loop      => "no",
            options   => undef,
            output    => "#transcode{vcodec=\"h264\"}:standard{access=http,mux=ts,dst=xxx.xxx.xxx.xxx:6690/cam1}",
            type      => "broadcast",
          },
        },
        schedule => undef,
      },
    }
    

    【讨论】:

      【解决方案2】:

      您可能正在尝试一些已经完成的事情 - 检查这个 SF 项目:

      http://sourceforge.net/projects/p5vlc/files/latest/download?source=files

      【讨论】:

      • 有帮助,但不完全是我需要的。我会尝试根据它写一些东西。谢谢。
      猜你喜欢
      • 1970-01-01
      • 2011-08-12
      • 1970-01-01
      • 1970-01-01
      • 2013-11-25
      • 2019-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多