English 中文(简体)
将线路改为每栏或平栏
原标题:converting lines to columns in perl or python
  • 时间:2012-04-24 03:48:26
  •  标签:
  • python
  • perl

我有这样的数据:

Re: Building A

Month
kWh
1
100
2
110
3
105


Re: Building B

Month
kWh
1
200
2
210
3
205

我想将其转换成每个建筑物的多个文本文件。 我的计划是:

  1. extract the values between the building delimiter line
  2. convert the lines into a table

就任务(1)而言,我试图像这样使用双管线操作员:

while( <DATA> ) {
  next unless /^Re: Building A/ .. /^Re: Building B/;
  my $line = $_;
  print $line;
}

但是,这并不奏效,因为以上所述只能显示A楼的数据。 这些数据涉及多个建筑物(其中约50座),因此,我需要稍加谨慎地这样做。 我没有开始做任务(2)。

任何帮助都将受到赞赏。

最佳回答

我愿这样做:

#!/usr/bin/perl
use strict;
use warnings;

my %buildings;

while (<DATA>) {
    chomp;
    $buildings{$1} = [] if /^Re: Building ([AB])/;
    push @{$buildings{$1}}, $_ if $_;
}

while (my ($building, $data) = each %buildings) {
    open(my $out,  > , "$building.txt") or die "Unable to open file for writing: $!
";

    for my $i (1 .. $#$data / 2) {
        print $out sprintf "%s	%s
", $data->[$i*2-1], $data->[$i*2];
    }
    close $out;
}

A.txt:

Month   kWh
1       100
2       110
3       105

B.txt:

Month   kWh
1       200
2       210
3       205
问题回答

我认为,你可以坐在一张桌子上,这样,我就告诉你,你要求做什么,我认为什么是好的。

$name = "";
$data = {}; 
open(IN, "build.txt");
foreach my $line (<IN>){
    if($line =~ /Re: (.*)
/) { # get building name
        $name = $1;
        $name =~ s/ /_/;
        $data->{$name} = []; # link to empty array
    } else {
        # make a new list and return to a list
        @{$data->{$name}} = (@{$data->{$name}}, $line); # add line to current building data
    }
}
close IN;
#
# write on file for each
#
foreach my $name (keys %{$data}){
    open(OUT, ">$name.txt");
    foreach my $line (@{$data->{$name}}){
        print OUT $line;
    }
    close OUT;
}
#
# or write into one file as a table
#
open(OUT, ">tabledata.txt");
foreach my $name (keys %{$data}){
    # because the data was not filtered on import we filter it now
    my $flag = 0;
    my @data; 
    foreach my $line (@{$data->{$name}}){
        if($line =~ /kWh/) {
            $flag = 1;
        } elsif($line =~ /^
$/){ # skip blanks
        } elsif($flag == 1) {     # skip the counters
            $flag++; 
        } elsif($flag > 1) {
            chomp($line);
            @data = (@data, $line);
            $flag = 1;
        }   
    }
    # print pretty rows
    my $format = "%20s" . ("%10d" x @data);
    print OUT sprintf($format, $name, @data) . "
";

}
close OUT;      

这为每个建筑物提供建筑名称。 “A.txt”大楼的一个例子是:

Month
kWh
1
100
2
110
3
105

表格文档称为表格数据。

          Building_A       100       110       105
          Building_B       200       210       205




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签