CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Write the data by preserving quote characters

 



pradeep1982
New User

Dec 8, 2014, 3:12 PM

Post #1 of 10 (4249 views)
Write the data by preserving quote characters Can't Post

Hi

I have my source data coming from webservice call. The data is comma delimited with quote characters. I would like to write the output to a text file by preserving the quote characters.

my input of webservice (from browser):
"$2,355",2355,"$2,355",Approved,Approved,Approved,Approve,davecass,"awadhwa, azin",,,2014-10-28,0,2014-10-28,prad

output to be the same
"$2,355",2355,"$2,355",Approved,Approved,Approved,Approve,davecass,"awadhwa, azin",,,2014-10-28,0,2014-10-28,dblee

$data has the output of webservice

Code
              my $csv = Text::CSV->new ({ 
binary => 1, # Allow special character. Always set this
auto_diag => 1, # Report irregularities immediately
sep_char => ',', #Separatrp
});
open (my $fh1,"<:encoding(utf8)",\$data) or die "$!";
my $firstLine = 1;
while (my $row = $csv->getline($fh1)) {
if($firstLine){
$firstLine = 0;
}
else{
open(my $fh2,'>>',$filename) or die "Could not open file '$filename' $!";
print $fh2 "$row->[0],$row->[1],$row->[2],$row->[3],$row->[4],$row->[5],$row->[13],$row->[16],$row->[17],$row->[18],$row->[19],$row->[20],$row->[21],$row->[22],$row->[23],$row->[24],$row->[26],$row->[29],$row->[30],$row->[31]\n";
close $fh2;
}
}
close $fh1;
}

This is not preserving quotes. How to preserve the quotes


(This post was edited by FishMonger on Dec 9, 2014, 10:49 AM)


FishMonger
Veteran / Moderator

Dec 8, 2014, 3:33 PM

Post #2 of 10 (4246 views)
Re: [pradeep1982] Write the data by preserving quote characters [In reply to] Can't Post

Don't use perl's built-in print function. Use the print method provided by the Text::CSV module.

http://search.cpan.org/~makamaka/Text-CSV-1.32/lib/Text/CSV.pm#print


Laurent_R
Veteran / Moderator

Dec 9, 2014, 9:59 AM

Post #3 of 10 (4229 views)
Re: [pradeep1982] Write the data by preserving quote characters [In reply to] Can't Post

I may have missed something, but if you want to print the output exactly as the input, why do you bother splitting the CSV into components?


FishMonger
Veteran / Moderator

Dec 9, 2014, 10:44 AM

Post #4 of 10 (4227 views)
Re: [Laurent_R] Write the data by preserving quote characters [In reply to] Can't Post

Based on the print statement the OP used, split was used so that some of the fields could be left out of the new file.

One issue I missed (due to the lack of code formatting/indentation) when I first read the post is that the new file is being reopened for each and every line in the source file. That's a very inefficient approach.


(This post was edited by FishMonger on Dec 9, 2014, 10:47 AM)


pradeep1982
New User

Dec 9, 2014, 12:22 PM

Post #5 of 10 (4220 views)
Re: [FishMonger] Write the data by preserving quote characters [In reply to] Can't Post

Actually, I would like to write subset of columns.Not all columns. There are columns such as comments which has wierd characters and we dont want that.

@FishMonger - This is a webservice call. We have to call that URL 'n' no of times, until I dont find any data. When there is no data i have to exit. But sure will check.

Using csv->print(), how can i refer to individual column
Error : Expected fields to be an array ref at...
if i use
$csv->print($fh2,$row{0});


FishMonger
Veteran / Moderator

Dec 9, 2014, 12:35 PM

Post #6 of 10 (4217 views)
Re: [pradeep1982] Write the data by preserving quote characters [In reply to] Can't Post


Quote
@FishMonger - This is a webservice call. We have to call that URL 'n' no of times, until I dont find any data. When there is no data i have to exit. But sure will check.

I'm not sure which portion of my response you're referring to here, but if it's regarding the opening of the $fh2 file handle, that should not be done inside the while (my $row = $csv->getline($fh1)) { loop. You should move the open call prior to the initialization of the loop.


Quote
Using csv->print(), how can i refer to individual column

Prior to the print statement you should either use the splice function to strip out the fields you don't want or use an array slice to create a new array ref containing the wanted fields. Then just pass the whole array ref instead of listing each field.


pradeep1982
New User

Dec 9, 2014, 1:58 PM

Post #7 of 10 (4213 views)
Re: [FishMonger] Write the data by preserving quote characters [In reply to] Can't Post

This is my new code

Code
use Getopt::Long; 
use File::Copy;
use Text::CSV;

my $filename = '/mydir/file.txt';
my $filename2 = '/mydir/file_formatted.csv';

unlink $filename;

for (my $i=1;$i<=2;$i++)
{
$WS_URL = "www.adesfhr.com/wpi.php?cmd=dsd&PageNumber=$i&pageSize=10&responseType=csv";
my $resp=$SSO->get($WS_URL);
my $data = $resp->content();
if ($data)
{
my $csv = Text::CSV->new ({
binary => 1, # Allow special character. Always set this
auto_diag => 1, # Report irregularities immediately
sep_char => ',', #Separatrp
});
open (my $fh1,"<:encoding(utf8)",\$data) or die "$!";
open(my $fh2,'>>',$filename) or die "Could not open file '$filename' $!";
my $firstLine = 1;
while (my @row = $csv->getline($fh1))
{
if($firstLine)
{
$firstLine = 0;
}
else{
# print qq("$row->[0]|$row->[4]\n)";
my @newrow=splice(@row,0,3);
print "@newrow\n";
$csv->print($fh2,$newrow[0]);
}
}
close $fh2;
close $fh1;
}
else
{
last ;
}
}


This is printing all hash values
ARRAY(0xa03945c)
ARRAY(0xa039594)
ARRAY(0xa03969c)
ARRAY(0xa0396a8)
ARRAY(0xa039678)
ARRAY(0xa039774)
ARRAY(0xa0394d4)
ARRAY(0xa039630)
ARRAY(0xa001160)
ARRAY(0xa0393c0)
ARRAY(0xa0049fc)
ARRAY(0xa0a2aa8)
ARRAY(0xa03957c)
ARRAY(0x9fdbfb4)
ARRAY(0x9fe7ea0)
ARRAY(0xa0049fc)
ARRAY(0x9fe7ce4)
ARRAY(0xa04e4c8)
ARRAY(0xa0908c8)
ARRAY(0xa03957c)

and writing nothing to file

Please suggest


(This post was edited by FishMonger on Dec 9, 2014, 2:08 PM)


FishMonger
Veteran / Moderator

Dec 9, 2014, 2:10 PM

Post #8 of 10 (4209 views)
Re: [pradeep1982] Write the data by preserving quote characters [In reply to] Can't Post

Please use the code tags so that the formatting will be retained. Your formatting needs lots of corrections...it's very difficult to read/follow.

I'll look over your code a little later when I have more time.


pradeep1982
New User

Dec 9, 2014, 5:18 PM

Post #9 of 10 (4179 views)
Re: [FishMonger] Write the data by preserving quote characters [In reply to] Can't Post

Thanks Ron. Will wait for yur comments. Really dont understand where its going wrong.


FishMonger
Veteran / Moderator

Dec 9, 2014, 6:00 PM

Post #10 of 10 (4167 views)
Re: [pradeep1982] Write the data by preserving quote characters [In reply to] Can't Post

Your code has multiple problems, some are minor but some aren't.

$csv->getline($fh1) returns an array ref, not an array.

Your splice statement and resulting array assignment will need to be adjusted to use array refs.

Your use of $firstLine is unnecessary. Just read 1 line in void context before the loop.

When you print the array ref, you need to pass the entire thing, not just the first element.

I used perltidy to clean up the formatting, but it still needs some work.

There are additional issues I didn't address, the ones I pointed are the most important.


Code
use Getopt::Long; 
use File::Copy;
use Text::CSV;

my $filename = '/mydir/file.txt';
my $filename2 = '/mydir/file_formatted.csv';

unlink $filename;

for ( my $i = 1 ; $i <= 2 ; $i++ ) {
$WS_URL =
"www.adesfhr.com/wpi.php?cmd=dsd&PageNumber=$i&pageSize=10&responseType=csv";
my $resp = $SSO->get($WS_URL);
my $data = $resp->content();
last if ! $data;
if ($data) {
my $csv = Text::CSV->new(
{
binary => 1, # Allow special character. Always set this
auto_diag => 1, # Report irregularities immediately
sep_char => ',', #Separatrp
}
);
open( my $fh1, "<:encoding(utf8)", \$data ) or die "$!";
open( my $fh2, '>>', $filename )
or die "Could not open file '$filename' $!";

<$fh1>;
while ( my $row = $csv->getline($fh1) ) {

# print qq("$row->[0]|$row->[4]\n)";
my $newrow = splice( @$row, 0, 3 );
print "@$newrow\n";
$csv->print( $fh2, $newrow );

}
close $fh2;
close $fh1;
}

}


This line may need to be changed depending on what results you're expecting. I'm not sure what you intended.

Code
my $newrow = splice( @$row, 0, 3 );



(This post was edited by FishMonger on Dec 9, 2014, 6:05 PM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives