#!/usr/bin/env perl
use strict;
use warnings;
use File::Find;
use Text::CSV;
my $semi_colon_csv = Text::CSV->new( { 'sep_char' => ';', } );
my $comma_csv = Text::CSV->new( {
'sep_char' => ',',
'eol' => "\n",
} );
open my $fh_output, '>', 'output.csv' or die $!;
sub convert {
my $file_name = shift;
open my $fh_input, '<', $file_name or die $!;
# header
my $row = $semi_colon_csv->getline($fh_input);
$comma_csv->print( $fh_output, [ @$row, $file_name ] );
while ( $row = $semi_colon_csv->getline($fh_input) ) {
pop @$row unless $row->[-1]; # remove trailing semi-colon from input
my ($token) = ( $file_name =~ /^([^_]+)/ );
$comma_csv->print( $fh_output, [ @$row, $token ] );
}
}
sub wanted {
return unless -f;
convert($_);
}
my $path = 'csv'; # assuming that all your CSVs are in ./csv/
find( \&wanted, $path );
#!/usr/bin/env perl
use strict;
use warnings;
use File::Find;
use Text::CSV;
my $semi_colon_csv = Text::CSV->new( { 'sep_char' => ';', } );
my $comma_csv = Text::CSV->new( {
'sep_char' => ',',
'eol' => "\n",
} );
open my $fh_output, '>', 'output.csv' or die $!;
sub convert {
my $file_name = shift;
open my $fh_input, '<', $file_name or die $!;
# header
my $row = $semi_colon_csv->getline($fh_input);
$comma_csv->print( $fh_output, [ @$row, $file_name ] );
while ( $row = $semi_colon_csv->getline($fh_input) ) {
pop @$row unless $row->[-1]; # remove trailing semi-colon from input
my ($token) = ( $file_name =~ /^([^_]+)/ );
$comma_csv->print( $fh_output, [ @$row, $token ] );
}
}
sub wanted {
return unless -f;
convert($_);
}
my $path = 'csv'; # assuming that all your CSVs are in ./csv/
find( \&wanted, $path );
#!/usr/bin/perl
use strict;
use warnings;
# Open input file
my $inputfile = shift or die("Usage: $0 <filename>\n\n");
open F, $inputfile or die("Could not open input file ($!)\n\n");
# Split filename into an array
my @tokens = split("_", $inputfile);
my $isFirstline = 1;
# Iterate each line in the file
foreach my $line (<F>) {
my $addition;
chomp($line); # Remove newline
# Add the complete filename to the line at first line
if ($isFirstline) {
$isFirstline = 0;
$addition = ",$inputfile";
} else { # Add first token for the rest of the lines
$addition = ",$tokens[0]";
}
# Split the data into @elements array
my @elements = split(";", $line);
# Join it using comma and add filename/token & a new line
print join(",", @elements) . $addition . "\n";
}
close(F);
You might want to try this quick & dirty Perl hack to convert the data:
#!/usr/bin/perl
use strict;
use warnings;
# Open input file
my $inputfile = shift or die("Usage: $0 <filename>\n\n");
open F, $inputfile or die("Could not open input file ($!)\n\n");
# Split filename into an array
my @tokens = split("_", $inputfile);
my $isFirstline = 1;
# Iterate each line in the file
foreach my $line (<F>) {
my $addition;
chomp($line); # Remove newline
# Add the complete filename to the line at first line
if ($isFirstline) {
$isFirstline = 0;
$addition = ",$inputfile";
} else { # Add first token for the rest of the lines
$addition = ",$tokens[0]";
}
# Split the data into @elements array
my @elements = split(";", $line);
# Join it using comma and add filename/token & a new line
print join(",", @elements) . $addition . "\n";
}
close(F);
However, this holds the complete contents of all CSV files in memory until it is ready to export at the end. Not feasible unless you have 64-bit Windows with lots of memory. :-)
发布评论
评论(5)
使用
Text::CSV
:程序
输出(输出.csv)
Using
Text::CSV
:Program
Output (output.csv)
不管你相信与否,它可能很简单:
如果您想将字段分隔符从分号更改为逗号:
仅包含第一个标记:
Believe it or not, it may be as simple as:
If you want to change the field separator from semicolons to commas:
To include only the first token:
您可能想尝试一下这个快速&肮脏的 Perl hack 来转换数据:
You might want to try this quick & dirty Perl hack to convert the data:
Perl 的 DBI 模块可以处理 CSV 文件(需要 DBD::CSV 模块)和 MySQL。只需将所有 csv 文件放在同一个目录中,然后像这样查询它们:
您可以查询 csv 文件(包括 JOIN 语句!)并将数据直接插入 MySQL。
Perl's DBI module can cope with CSV files (DBD::CSV module required) and MySQL. Just put all your csv files in the same dir, and query them like this:
Yo can query csv files (including JOIN statements!) and insert data directly into MySQL.
这是在 PowerShell 中执行此操作的一种方法:
如果文件的大小不是那么大,我建议采用此路线:
但是,这会将所有 CSV 文件的完整内容保存在内存中,直到准备好在结尾。除非您有具有大量内存的 64 位 Windows,否则不可行。 :-)
This is one way to do it in PowerShell:
If the size of the files weren't so potentially large I would suggest going this route:
However, this holds the complete contents of all CSV files in memory until it is ready to export at the end. Not feasible unless you have 64-bit Windows with lots of memory. :-)