473,396 Members | 1,827 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 3768
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,556 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,556 Expert Mod 16PB
8 - )
Jul 28 '08 #9

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

Similar topics

0
by: Mike Bobbitt | last post by:
I've recently decided to switch from compiled Apache/PHP to RPM's of both and I'm having a problem I can't seem to solve. I have a Perl script called php_include.cgi that parses a PHP file for...
3
by: Claudio Grondi | last post by:
Is there an already available script/tool able to extract records and generate proper HTML code out of the data stored in the Wikipedia SQL data base? e.g. converting all occurences of ] to <a...
1
by: Wet Basement | last post by:
I am passing data to a putcart.pl , parsing it, then building the query string to put the item in the shopping cart: $cartStr =...
6
by: Spamtrap | last post by:
I only work in Perl occasionaly, and have been searching for a solution for a conversion, and everything I found seems much too complex. All I need to do is take a simple text file and copy...
3
by: nan | last post by:
Hi All, I am trying to connect the Database which is installed in AS400 using DB2 Client Version 8 in Windows box. First i created the Catalog, then when i selected the connection type...
3
by: josh.kuo | last post by:
Sorry about the subject, I can't think of a better one. I recently wrote some PHP classes that I think might be of interest to this group. Since I have been reaping the benefits of reading news...
4
by: Shreedhar Naik | last post by:
Hi, I am using XML::Simple to parse the XML file that XML file contains the "." in some of its tags. But while executing the script i am getting the error from Perl Script like "Use of...
1
by: Vic | last post by:
I have a c program which writes mac address entries onto a text file. Text file when opened in vim looks like this index mac 1 ^@^@^Q^@^@^A ascii of (00.00.11.00.00.01) 2 ^@^@^Q^@^@^B...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.