开发者

modify csv file before import

I have a csv file that contains a "location" field. I need to import this field into Drupal with node import, but I have to import the address field into different f开发者_如何学运维ields. One for "postal code", another for "street" etc.

The "location" field contains the whole address with a '|' delimiter in between - like this: "street street no | postal code city"

How would I best change the csv file before I import it? I only have experience with php, and I don't know much about perl or python scripts, but I have a hunch that it would be best to write a script that can parse the csv file and change the "location" field into new fields.

Is that about right? I would be grateful for some suggestions as to where I should start - thanks a lot


You don't give an example line. You might have trouble if you don't know Perl.
In general, there are good Csv modules on CPAN, this may be needed to discerne embedded
field delimeter char(s) within the field itself. Otherwise, read in the file line by line,
modify the line then write it out to a new file. Something like this (untested) ..

use strict;
use warnings;

open my $infile,  '<', 'filein.csv'  or die "error opening infile $!";
open my $outfile, '>', 'fileout.csv' or die "error opening outfile $!";

while (<$infile>) {
   my @fields = split /,|\|/, $_;
   print $outfile join(',', @fields), "\n";
}
close $outfile;
close $infile;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜