0

私は次のようなデータセットを持っています - 私は正規表現に非常に慣れておらず、いくつかの貧弱な試みにもかかわらず、「ツリーを歩く」という知識がありません - Excel の列へのテキストは、さまざまな用語クラス/タグの無意味な組織のために役に立ちませんEFFECT_DATA フィールドと手動で調整することによって導入されたエラーで。

サンプルデータ

ROW_ID|NAME   | UNORDERED_CSV_CONCATD_TAG_DATA_STRING

123456|Prod123|"Minoxidistuff [MoA], Direct [PE], Agonists [EPC]"
123457|Prod124|"Minoxion [Chem], InterferonA [EPC], Delayed [PE]"

123458|Prod125|"Anotherion [EPC], Direct [MoA], Agonists [EPC]"
123459|Prod126|"Competitor [PE], Progestin [EPC], Agonists [EPC]"
123460|Prod127|"Minoxidistuff [Chem]"

必要なデータ出力のサンプル:

PRODUCT|EPC      |
Prod125|Antherion|
Prod125|Agonists |

PRODUCT|CMPD         |
Prod127|Minoxidistuff|
Prod124|Minoxion    |

product[i]tag[j] のすべてのタグについて、それが理にかなっていれば、基本的に ea. CSVD_TAG_DATA フィールドの順序が正しくなく、複数のタグが含まれています (目的の用語の末尾に.

私は多次元ハッシュアプ​​ローチを開始しています。つまり、私の肉屋の正規表現擬似コードを許してください。

どうもありがとう。

4

1 に答える 1

1

これがPerlメソッドです。以下のコードをparser.plとして保存します。perl parser.pl data.csvdata.csvがデータ​​ファイルの名前であるように実行します。(または実行可能にして実行します./parser.pl data.csv。)

#!/usr/bin/perl -w

use strict;

# Take in the first arguement as the file
my $file = $ARGV[0];

# open a filehandle
open (my $fh, '<', $file);

# Well predefine a hashref
my $products = {};

# Loop through the file
while (<$fh>) {

        # remove line breaks
        chomp;

        # split into our primary sections
        my ($id, $product, $csv) = split(/\|/);

        # skip a header line
        next if ($id =~ /\D/);

        # remove the quotes
        ($csv) = ($csv =~ /"(.*)"/);

        # split the CSV an a comma possibly followed by a space
        my @items = split(/,\s*/, $csv);

        # loop through each item in the csv
        foreach my $item(@items) {

                # Our keys and values are reversed!
                my ($value,$key) = ($item =~ /(.*)\[(.*)\]/);

                # Remove trailing whitespace
                $value =~ s/\s+$//;

                # If the arrayref does not exist then create it
                # Otherwise add to it
                if (!exists($products->{$key}->{$product})) {
                        $products->{$key}->{$product} = [$value];
                } else {
                        push(@{$products->{$key}->{$product}}, $value);
                }

        }

}

# We have a nicely formed hashref now. Loop through and print how we want

foreach my $key(keys %$products) {

        # Header for this section
        print "PRODUCT|$key\n";

        # Go through each product and print the different values
        foreach my $product(keys %{$products->{$key}}) {

                while (my $value = shift(@{$products->{$key}->{$product}})) {
                        print "$product|$value\n";
                }

        }

        # Add a space to divide the groups cleanly
        print "\n";

}

サンプル出力:

PRODUCT|MoA
Prod123|Minoxidistuff
Prod125|Direct

PRODUCT|Chem
Prod127|Minoxidistuff
Prod124|Minoxion

PRODUCT|PE
Prod123|Direct
Prod124|Delayed
Prod126|Competitor

PRODUCT|EPC
Prod123|Agonists
Prod124|InterferonA
Prod126|Progestin
Prod126|Agonists
Prod125|Anotherion
Prod125|Agonists
于 2012-11-30T08:41:28.333 に答える