0

所以我在 perl 中有这段代码来解析 xml,我希望 perl 文件打印到文本文件中,以便所有内容都在列中,如下所示:

SpectrumIndex TotalIonCount Duration Spectrum 
Integer Float Float mass,abundance;mass1,abundance1;mass 2,abundance1

这是xml代码:

<message>
       <cmd id="result_data">
          <result-file-header>
                 <path>String</path>
                 <duration>Float</duration>
                 <spectra-count>Integer</spectra-count>
          </result-file-header>
          <scan-results count="Integer">
                 <scan-result>
                 <spectrum-index>Integer</spectrum-index>
                 <time-stamp>Integer</time-stamp>
                 <tic>Float</tic>
                 <start-mass>Float</start-mass>
                 <stop-mass>Float</stop-mass>
                 <spectrum count="Integer">mass,abundance;mass1,abundance1;
                 mass2,abundance2;mass6,abundance6</spectrum>
                 </scan-result>
                 <scan-result>
                 <spectrum-index>Integer</spectrum-index>
                 <time-stamp>Integer</time-stamp>
                 <tic>Float</tic>
                 <start-mass>Float</start-mass>
                 <stop-mass>Float</stop-mass>
                 <spectrum count="Integer">mass3,abundance3;mass4,abundance4;
                 mass5,abundance5</spectrum>
                 </scan-result>
            </scan-results>
    </cmd>
</message>

但是使用以下 perl 代码:

#!/usr/bin/perl-w
#example to write to text
my $file = "gapiformat.txt";
unless(open FILE, '>'.$file) {
    die "\nUnable to create $file\n";
}
use strict;
use warnings;
use XML::Simple;
use Data::Dumper;


 my $values= XMLin('samplegapi.xml',ForceArray => ['scan-result','result-file-header']);


my $results = $values->{'cmd'}->{'scan-results'}->{'scan-result'};
my $results1= $values->{'cmd'}->{'result-file-header'};

printf FILE ("%-s %-s %-s %s\n","SpectrumIndex","TotalIonCount","Spectrum","Duration");
for my $data (@$results) {
    my $spectrum=$data->{spectrum};
    for my $data1 (@$results1){    
    printf FILE ("%-s %-s %-s %-s\n",$data->{"spectrum-index"}, $data->{tic}, $spectrum          
->{'content'},$data1->{duration});
    }   
  }

它为文本文件提供以下输出:

 SpectrumIndex TotalIonCount Spectrum Duration
 Integer Float mass,abundance;mass1,abundance1;
                 mass2,abundance2;mass6,abundance6 Float
 Integer Float mass3,abundance3;mass4,abundance4;
                 mass5,abundance5 Float

任何帮助表示赞赏!

4

3 回答 3

1

只需在 printf 中输入您想要的字符数:例如

printf FILE ("%20s %20s %40s %10s\n",...)
于 2013-06-07T02:04:07.523 回答
0

我想通了...我刚用过:

my $content=$spectrum->{'content'};
$content=~s/\s+$//g

这成功了

于 2013-06-19T21:26:54.443 回答
0
  1. 在现代 perl 中:

    • 您使用的三个参数形式open()
    • 您使用my变量而不是裸字文件句柄
    • 当出现问题时,您会输出系统错误消息 ( $!):

    open my $INFILE, '<', $fname
        or die "Couldn't open $fname: $!";
    
  2. 您必须逐行打印。您不能先打印一列,然后再打印另一列。
    所以当你这样做时:

    printf FILE (
        "%-s %-s %-s %-s\n",
        $data->{"spectrum-index"},
        $data->{tic},
        $spectrum->{'content'},
        $data1->{duration}
    );
    

    ...当然,所有这些数据都将放在一行上。

于 2013-06-07T00:24:30.613 回答