replace character in CSV field..

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jschenker01
    New Member
    • Jan 2014
    • 4

    replace character in CSV field..

    This is a chunk of the scrip I use to FTP a csv file..
    The problem I am having is the fields 19 & 30.

    In a couple of the records there are quotes. (ie. 18" wheels)
    How can I have the script escape it where it (ie. 18\" wheels)?

    Thanks for any help!

    ----------------------
    Code:
    #!/usr/bin/perl --
    #use LWP;
    use CGI::Carp qw(fatalsToBrowser);
    use strict;
    use LWP::Simple;
    use Net::FTP;
    
    
    print "Content-type: text/html\n\n";
    
    print "Fetching Data<BR>\n";
    
    my $cmd="http://www.websitesfordealers.com/cgi-bin/data.pl?data.year=between+1983+and+2030&_layout=dodah&_cgifunction=Search&data.vin=%21%21&data.block=%21&data.make=ACURA+or+AUDI+or+BMW+or+BUICK+or+CADILLAC+or+CHEVROLET+or+CHRYSLER+or+DAEWOO+or+DAIHATSU+or+DATSUN+or+DODGE+or+EAGLE+or+FERRARI+or+FORD+or+GEO+or+GMC+or+HONDA+or+HUMMER+or+HYUNDAI+or+INFINITI+or+ISUZU+or+JAGUAR+or+JEEP+or+KIA+or+LANDROVER+or+LEXUS+or+LINCOLN+or+MAZDA+or+MERCEDES+or+MERCURY+or+MERKUR+or+MINI+or+MITSUBISHI+or+NISSAN+or+OLDSMOBILE+or+PLYMOUTH+or+PONTIAC+or+PORSCHE+or+SAAB+or+SATURN+or+SCION+or+SUBARU+or+SUZUKI+or+TOYOTA+or+VOLKSWAGEN+or+VOLVO";
    
    my $doc=get $cmd;
    $doc =~ s/ImageURLs/ImageURLs\n/;
    $doc =~ s/\r/\n/g;
    Last edited by Rabbit; Jan 24 '14, 04:58 AM. Reason: Please use [CODE] and [/CODE] tags when posting code or formatted data.
  • RonB
    Recognized Expert Contributor
    • Jun 2009
    • 589

    #2
    Your code snippet doesn't show anything related to parsing/processing a csv file.

    When dealing with csv files, you should use a csv parser module.
    Text::CSV
    Text::CSV_XS

    The module will handle the escaping of the quotes

    Comment

    • jschenker01
      New Member
      • Jan 2014
      • 4

      #3
      Forgive my ignorance...
      I'll try to explain better what I have going on, but I'll probably botch it. :)

      I have another cgi script that creates the layout for my csv.
      The PL script I have runs the cgi script, then saves the csv file and FTP's it.

      What I'm looking for is a snippet I can add to my existing pl script to replace the quotes within fields 19 & 30 with /"

      Here is my full PL script.. I should have loaded it the first time.

      Code:
      #!/usr/bin/perl --
      #use LWP;
      use CGI::Carp qw(fatalsToBrowser);
      use strict;
      use LWP::Simple;
      use Net::FTP;
      
      
      print "Content-type: text/html\n\n";
      
      print "Fetching Data<BR>\n";
      
      my $cmd="http://www.websitesfordealers.com/cgi-bin/data.pl?data.year=between+1983+and+2030&_layout=dodah&_cgifunction=Search&data.vin=%21%21&data.block=%21&data.make=ACURA+or+AUDI+or+BMW+or+BUICK+or+CADILLAC+or+CHEVROLET+or+CHRYSLER+or+DAEWOO+or+DAIHATSU+or+DATSUN+or+DODGE+or+EAGLE+or+FERRARI+or+FORD+or+GEO+or+GMC+or+HONDA+or+HUMMER+or+HYUNDAI+or+INFINITI+or+ISUZU+or+JAGUAR+or+JEEP+or+KIA+or+LANDROVER+or+LEXUS+or+LINCOLN+or+MAZDA+or+MERCEDES+or+MERCURY+or+MERKUR+or+MINI+or+MITSUBISHI+or+NISSAN+or+OLDSMOBILE+or+PLYMOUTH+or+PONTIAC+or+PORSCHE+or+SAAB+or+SATURN+or+SCION+or+SUBARU+or+SUZUKI+or+TOYOTA+or+VOLKSWAGEN+or+VOLVO";
      
      my $doc=get $cmd;
      $doc =~ s/ImageURLs/ImageURLs\n/;
      $doc =~ s/\r/\n/g;
      
      my $filename="websitesfordealers.csv";
      
      print "Writing to feeds/dodah/$filename<BR>\n";
      open(FILE,">feeds/dodah/$filename") or die "Could not open $filename for writing $!";
      print FILE $doc;
      close FILE;
      
      print "Connecting to dodah<BR>\n";
      my $ftp = Net::FTP->new("uploads.domain.com", Debug => 0)
            or die "Cannot connect to some.host.name: $@";
      
      $ftp->login("something",'somethingelse')
            or die "Cannot login ", $ftp->message;
      
      $ftp->put("feeds/dodah/$filename")  or die "Cannot put file ", $ftp->message;;
      
      $ftp->quit;
      
      print "<PRE>File Transferred OK</PRE>";
      Last edited by RonB; Jan 25 '14, 05:26 AM.

      Comment

      • RonB
        Recognized Expert Contributor
        • Jun 2009
        • 589

        #4
        The proper solution would be what I suggested in my first response. Filter the lines of the file through Text::CSV and let it handle the quotes.

        If you don't want to do that, then simply apply a regex to those 2 fields.
        Code:
        s/"/\\"/g for $field[19], $field[30];
        The next question you should ask would be "how do I loop over the file contents and split it into fields when it's in a $doc scalar?". Do you know how to do that?

        Comment

        Working...