0

我正在调用一个 web 服务,它返回一个逗号分隔的数据集,其中包含不同的列和多个文本限定的行(第一行表示列名)。我需要将每一行插入数据库,同时连接不同的行。

数据是这样返回的

"Email Address","First Name","Last Name", "State","Training","Suppression","Events","MEMBER_RATING","OPTIN_TIME","CLEAN_CAMPAIGN_ID"

"scott@example.com","Scott","Staph","NY","Campaigns and activism","Social Media","Fundraiser",1,"2012-03-08 17:17:42","Training"

State和之间最多可以有 60 列Member_Rating,这些字段中的数据将被连接并插入到一个数据库列中。列表中的前四个字段和后三个字段将始终相同。我不确定解决这个问题的最佳方法。

4

1 回答 1

1

我不确定此解决方案是否符合您的需求。但愿如此。这是一个perl脚本,-除了前四个和后三个之外,所有字段都用空格包围。它使用非标准模块,Text::CSV_XS必须使用CPAN或类似工具安装。

内容infile

"Email Address","First Name","Last Name","State","Training","Suppression","Events","MEMBER_RATING","OPTIN_TIME","CLEAN_CAMPAIGN_ID"
"scott@example.com","Scott","Staph","NY","Campaigns and activism","Social Media","Fundraiser",1,"2012-03-08 17:17:42","Training"

内容script.pl

use warnings;
use strict;
use Text::CSV_XS;

my $csv = Text::CSV_XS->new({ 
        allow_whitespace => 1,
});

open my $fh, q[<], $ARGV[0] or die qq[Open: $!\n];

while ( my $row = $csv->getline( $fh ) ) {
        my $concat = join q[ - ], (@$row)[4 .. @$row-4];
        splice @$row, 4, scalar @$row - (3 + 4), $concat;
        $csv->print( \*STDOUT, $row );
        print qq[\n];
}

像这样运行它:

perl script.pl infile

具有以下输出:

"Email Address","First Name","Last Name",State,"Training - Suppression - Events",MEMBER_RATING,OPTIN_TIME,CLEAN_CAMPAIGN_ID
scott@example.com,Scott,Staph,NY,"Campaigns and activism - Social Media - Fundraiser",1,"2012-03-08 17:17:42",Training
于 2012-04-23T18:37:54.257 回答