We have a script that creates a .csv file that in turn is fed into a python script. Our .csv file is being generated with a line feed at the bottom, and this is hammering the python script. Is there a way to get rid of that extra line that is being generated?

Thanks
DS

So are you in control of either the producer or consumer, source code wise?

Is the producer a perl script?

Yes...we produce the offending .csv file. Can't seem to get it to create without their being a blank line at the bottom.

Sorry, here is the code. Was hoping there was something obvious to use.

#!/awips/dev/per15/bin/perl
#
################################################################################
#
#  This program will parse out MAX, MIN, PRECIP, SNOWFALL, SNOW DEPTH from 
#  the file WRKPCP and place in a file that will be used for GIS 
#  GRAPHICS injestion.
#
################################################################################
#
# delete any outdated TOPWRKPCP files
#
unlink </awips/dev/localapps/kmltools/WRKPCP>;
#
# call new file into program directory.
#
#
$pil1="TOPWRKPCP";
$textdbCMD = "textdb -r ".$pil1." > /awips/dev/localapps/kmltools/WRKPCP";
$textdb = `$textdbCMD`;
#
# copy all of the lines in WRKPCP first
#
$i=1;
open (PCP1, "< /awips/dev/localapps/kmltools/WRKPCP");
while (<PCP1>) {
 $line1[$i]=$_;
 #print "$line1[$i]\n";
 $count1=$i;
 ++$i;
}
close (PCP1);
#
# copy all lat and long
#
$k=1;
open (PCP2, "< /awips/dev/localapps/kmltools/pcplatlon.txt");
while (<PCP2>) {
 $line2[$k]=$_;
 #print "$line2[$k]\n";
 $count2=$k;
 ++$k;
}
close (PCP2);
#
#  ....start parsing
#
#
for ($i=1; $i<=$count1; ++$i){

     if ($line1[$i] =~/.{5}?\:/){
         #print "hey this should  be a line\n";
         $line1[$i] =~/(.{5}?)\:(.{26}?)\:.{2}?(.{5}?).{1}?(.{5}?).{3}?(.{5}?).{3}?(.{4}?).{2}?(.{2}?)/;
         $site[$i]=$1;
	 $location[$i]=$2;
	 $high[$i]=$3;
	 $low[$i]=$4;
	 $precip[$i]=$5;
	 $snowfall[$i]=$6;
	 $snowdepth[$i]=$7;
     }
}
for ($k=1; $k<=$count2; ++$k){

     if ($line2[$k] =~/^[A-Z]{3}.+?/ || $line2[$k] =~/^[A-Z]{5}.+?/){
         #print "hey this should  be a line2\n";
         $line2[$k] =~/(.{5}?).+?([A-Z]{3}).+?([0-9]{2}\.[0-9]{4}).+?(\-[0-9]{2}\.[0-9]{4}).+?/ || $line2[$k] =~/(.{3}?).+?([A-Z]{3}).+?([0-9]{2}\.[0-9]{4}).+?(\-[0-9]{2}\.[0-9]{4}).+?/;
         $site3[$k]=$1;
	 $cwa[$k]=$2;
	 $lat[$k]=$3;
	 $lon[$k]=$4;
     }
}
#
# ...post process to remove empty characters
#
for ($i=1; $i<=$count1; ++$i){
     #print "site= $site[$i]\n";
     if ($site[$i]=~/[A-Z]{4}[0-9]{1}/){
         #print "this 5 string line must occur\n";
         $site1[$i]= $site[$i]; 
     }
     else {
         $site[$i]=~/([A-Z]{3}).+?/;
	 $site1[$i]= $1;
     }
     if ($high[$i] eq "     "){
        $high1[$i]= "";
     }
     else {
        $high1[$i]=int($high[$i]);
     }
     if ( $low[$i] eq "     "){
         $low1[$i]= "";
     }
     else {
          $low1[$i]=int($low[$i]);
     }
}
#
#
#
$num=1;
# if highs equal number then keep        
#
#
#   eliminate blank lines.............
#
#
#
@num=0;
$sline=1;
for ($i=1; $i<=$count1; ++$i){
     if ($site[$i]=~/[A-Z]{3}.+?/ || $site[$i]=~/[A-Z]{5}.+?/){
         $num[$sline]=$i;
	 $tline=$sline;
	 #print "tline=  $tline\n";
	 ++$sline;
     }
}

$m=1;
$spacer2="  ";
for ($j=$num[1]; $j<=$num[$tline]; ++$j){
     $site2[$m]=$site1[$j];
     if ($site1[$j]=~/[A-Z]{4}[0-9]{1}/){
         $site5[$m]=$site1[$j];
     }
     else {
          $site5[$m]=$site1[$j].$spacer2;
     }
     $location2[$m]=$location[$j];
     $high2[$m]=$high1[$j];
     $low2[$m]=$low1[$j];
     $precip2[$m]=$precip1[$j];
     $snowfall2[$m]=$snowfall1[$j];
     $snowdepth2[$m]=$snowdepth1[$j];
     $endnum=$m;
     ++$m;
}
for ($i=1; $i<=$endnum; ++$i){
     $location3[$i]=$location2[$i];
     #print "location3= $location3[$i]\n";
     if ( $location3[$i]=~/^\s+.*?\s+/){
      #print "this is a match\n";
      $location3[$i]=~/^\s+(.*?)\s+/;
      $location4[$i]=$1;
     }
     #print "location= $location2[$i] location without trailing space= $location4[$i]\n"; 
}
$num=1;
# if highs equal number then keep      
for ($i=1; $i<=$endnum; ++$i){
    if ($high2[$i]=~/[0-9]{2}/ || $high2[$i]=~/[0-9]{3}/ ){
        $lnum[$num]=$i;
	#print "lnum= $lnum[$num] high1= $high1[$i]\n";
	++$num;
    }
}

for ($i=1; $i<=$endnum; ++$i){
     #print "STD     LOCATION                     HIGH   LOW    PCPN   SNOWFALL   SNOWDEPTH\n";
     #print "$site2[$i]      $location2[$i]         $high2[$i]     $low2[$i]    $precip2[$i]     $snowfall2[$i]      $snowdepth2[$i]\n";
     #print "cwa= $cwa[$i]    lattitude= $lat[$i]    longitude= $lon[$i]\n";
}
# station ID's
$stnid[1]="KS-CD-99";
$stnid[2]="KS-LY-99";
$stnid[3]="KS-SN-99";
$stnid[4]="KS-DG-99";

265 of the stnid

#
#
$spacer=" ";
$spacer3=",";
$newsnow="NA";
$snowdepth="NA";
$waterequiv="NA";
#
for ($i=1; $i<=$num; ++$i){
     for ($j=1; $j<=$endnum; ++$j){
          #print "site2=$site2[$i]and site3=$site3[$j]end\n";
	  if ($site2[$lnum[$i]] eq $site3[$j]){
	      #print "hey there site2= $site2[$lnum[$i]] site3= $site3[$j] \n";
	      #print "lnum=$lnum[$i] i= $i\n";
              $finline1[$i]=$stnid[$lnum[$i]].$spacer3.$location4[$lnum[$i]].$spacer3.$lat[$j].$spacer3.$lon[$j].$spacer3.$high2[$lnum[$i]].$spacer3.$low2[$lnum[$i]].$spacer3.$newsnow.$spacer3.$snowdepth.$spacer3.$waterequiv;
	    #$finline[$i]=$site5[$i].$spacer3.$cwa[$j].$spacer3.$lat[$j].$spacer3.$lon[$j].$spacer3.$location2[$i].$spacer3.$high2[$i].$spacer3.$low2[$i].$spacer3.$precip2[$i].$spacer3.$snowfall2[$i].$spacer3.$snowdepth2[$i];
	    #$finline[$i]=$site5[$i].$spacer3.$cwa[$j].$spacer3.$lat[$j].$spacer3.$lon[$j].$spacer3.$location2[$i].$spacer3.$high2[$i].$spacer3.$low2[$i].$spacer3.$precip2[$i].$spacer3.$snowfall2[$i].$spacer3.$snowdepth2[$i];
	    #print "$finline[$i]\n";
	  }
      }  
     #print "STD     LOCATION                     HIGH   LOW    PCPN   SNOWFALL   SNOWDEPTH\n";
     #print "$site2[$i]      $location2[$i]         $high2[$i]     $low2[$i]    $precip2[$i]     $snowfall2[$i]      $snowdepth2[$i]\n";
     #print "cwa= $cwa[$i]    lattitude= $lat[$i]    longitude= $lon[$i]\n";
}
$q=1;
for ($j=1; $j<=$num; ++$j){
     if ($finline1[$j]=~/KS\-[A-Z]{2}\-[0-9]{2}.+/){
     #print "hey this has KS in it\n";
     $num1=$q;
     $finline[$q]=$finline1[$j];
     print "q= $q and $finline[$q]\n";
     ++$q;
     } 
}

#$finline[0]="SID,CWA,LATITUDE,LONGITUDE,LOCATION,HIGH,LOW,PCPN,SF,SD";
#print "finline= $finline[0]\n";
#
#... format
#
#unlink </data/local/GIS/WRKGIS.txt>;
unlink </awips/dev/localapps/kmltools/WRKGIS1.csv>;
#
#open (MLB2, "> /awips/dev/localapps/kmltools/WRKGIS1.txt");
#print @finline;
# write(MLB1);
#format MLB1=
#SID,CWA,LATITUDE,LONGITUDE,LOCATION,HIGH,LOW,PCPN,SF,SD
#.
close (MLB1);

#close(my @finline);

print " num1= $num1\n";

#$q=$q-1;

#print " the new q= $q\n";

#open (MLB2, ">> /data/local/GIS/WRKGIS.txt");
open (MLB2, ">> /awips/dev/localapps/kmltools/WRKGIS1.csv");
for ($i=1; $i<=$num1; ++$i){
 if ($finline[$i] =~/[A-Z]+/ ){
 write(MLB2);
format MLB2=
@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$finline[$i];
.
 }
}
close (MLB2);
#
#$i=1;
#open (PCP5, "< /awips/dev/localapps/kmltools/WRKGIS.csv");
#while (<PCP5>) {
# if ($line2[$i] =~/[A-Z]+/ ){
#  $line2[$i]=$_;
#  $line2[$i] >> "/awips/dev/localapps/kmltools/WRKGIS1.csv";
#  print "$line2[$i]\n";
#  $count1=$i;
#  ++$i;
# }
#}
#close (PCP5);

#open (MLB3, ">> /awips/dev/localapps/kmltools/WRKGIS1.csv");
#while (<MLB3> {
#    print @line2;
#}
#close (MLB3);
#
#...send out
#    	 
##$pil10="TOPWRKGIS";
##$textdbCMD = "textdb -w ".$pil10." </home/gargan/WRKGIS";
##$textdb = `$textdbCMD`;

just a guess, in this block of code:

#open (MLB2, ">> /data/local/GIS/WRKGIS.txt");
open (MLB2, ">> /awips/dev/localapps/kmltools/WRKGIS1.csv");
for ($i=1; $i<=$num1; ++$i){
 if ($finline[$i] =~/[A-Z]+/ ){
 write(MLB2);
format MLB2=
@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$finline[$i];
.
 }
}

change $i<=$num1 to $i<$num1

We attempted that but it did not work. We are now looking at how to handle it the blank line through the python script we feed the .csv file to. Thanks

OK, good luck.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.