469,289 Members | 2,330 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,289 developers. It's quick & easy.

Code for Conversion of csv file into {" "},{" "} using perl script

Hi guys,
Can anyone help me convert a csv file into the above format i am looking out for a line by line conversion with the help of perl scripting.

With Regards,
Anupam
Oct 23 '06 #1
8 3477
miller
1,089 Expert 1GB
Takes in a filename from the command prompt and outputs a new file with the extension ".new" with the given format. You can decode this code on your own.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4.  
  5. require Text::CSV;
  6.  
  7. my $inFile = shift;
  8. my $outFile = $inFile . '.new';
  9.  
  10. open(IN, $inFile) or die "open $inFile: $!";
  11. open(OUT, ">$outFile") or die "open >$inFile: $!";
  12.  
  13. my $csv = Text::CSV->new;
  14.  
  15. while (my $line = <IN>) {
  16.     if ($csv->parse($line)) {
  17.         my $newLine = join ",", map {
  18.             s/\"/\"\"/; # Escape all double quotes
  19.             "{\"$_\"}" # New String Enclosure
  20.         } $csv->fields;
  21.  
  22.         print OUT $newLine . "\n";
  23.     } else {
  24.         print "parse() failed on argument: ", $csv->error_input, "\n";
  25.     }
  26. }
  27.  
  28. close(IN) or die "close $inFile: $!";
  29. close(OUT) or die "close $inFile: $!";
  30.  
  31. 1;
  32.  
  33. __END__
  34.  
Oct 23 '06 #2
Hi Miller,
I have just changed my $infile = test This is my test.csv file I am getting the following error :-

Bareword "test" not allowed while "strict subs" in use at check.pl line 7.
Execution of check.pl aborted due to compliations errors.

The following given below is the change in the code I made
#!/usr/bin/perl

use strict;

require Text::CSV;

my $inFile = test;
my $outFile = $inFile . '.new';

open(IN, $inFile) or die "open $inFile: $!";
open(OUT, ">$outFile") or die "open >$inFile: $!";

my $csv = Text::CSV->new;

while (my $line = <IN>) {
if ($csv->parse($line)) {
my $newLine = join ",", map {
s/\"/\"\"/; # Escape all double quotes
"{\"$_\"}" # New String Enclosure
} $csv->fields;

print OUT $newLine . "\n";
} else {
print "parse() failed on argument: ", $csv->error_input, "\n";
}
}

close(IN) or die "close $inFile: $!";
close(OUT) or die "close $inFile: $!";

1;

__END__
Oct 23 '06 #3
miller
1,089 Expert 1GB
Hi Miller,
I have just changed my $infile = test This is my test.csv file I am getting the following error :-

Bareword "test" not allowed while "strict subs" in use at check.pl line 7.
Execution of check.pl aborted due to compliations errors.
If you got that error, then maybe you did something "wrong"? Maybe? Dude, I gave you all the code that you could possible need for this. I can't teach you perl over the forums, nor translate all the ways that you can screw up the code that I gave you into meaningful error messages.

Nevertheless, this one time. ok.

Bareword "test" not allowed at line 7. Hrmmm.... It's telling us that there is a problem at line 7. Wait! You changed something at line 7. What was it that we changed? Oh, that's right we added the word test. Oh, and look here, the error even says says bareword "test" not allowed? Maybe that was bad. Well golly shucks, maybe I should include any string in quotes?

my $infile = "test";

There, now the code will look for some input file named "test". Alternatively, you could just leave the code alone and input the filename from the command prompt.

perl check.pl test

Either way it would help if you took the time to learn at least an rudimentary understanding of perl before asking questions. There are no ignorant questions, just ignorant questioners. :)
QED
Oct 23 '06 #4
md2008
1
[ ad hominem attack deleted ]

Jos (moderator)
Jul 22 '08 #5
KevinADC
4,059 Expert 2GB
[ ad hominem attack deleted ]

Jos (moderator)
hehehe...... I missed the action.
Jul 22 '08 #6
NeoPa
32,173 Expert Mod 16PB
hehehe...... I missed the action.
Me too - and knowing your links with Miller I tried to find a copy to PM you. Unfortunately I think it's lost (a bit like the member really :-D).
Jul 26 '08 #7
KevinADC
4,059 Expert 2GB
Me too - and knowing your links with Miller I tried to find a copy to PM you. Unfortunately I think it's lost (a bit like the member really :-D).
Jeff filled me in, thanks.
Jul 26 '08 #8
NeoPa
32,173 Expert Mod 16PB
8 - )
Jul 28 '08 #9

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Mike Bobbitt | last post: by
6 posts views Thread by Spamtrap | last post: by
3 posts views Thread by josh.kuo | last post: by
1 post views Thread by Vic | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.