Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re^2: Read text file - Encoding problem?

by better (Acolyte)
on Mar 17, 2013 at 10:53 UTC ( #1023899=note: print w/ replies, xml ) Need Help??

in reply to Re: Read text file - Encoding problem?
in thread Read text file - Encoding problem?

Hi McA,

Thanks for that script. It seems that it is not a problem of encoding. I checked both text files, which are used to be read into a filehandle. There is a difference regular ocurring: Each line of the "bad" text file which was parsed from the csv and which is not working has a -d- at its end, while the lines of the "good" text file which is working with my script have not:

eg:  I C 7700 -> 49-20-43-20-37-37-30-30-    #good

   I C 7700 -> 49-20-43-20-37-37-30-30-d     #bad

and what I get reading the directory:

  I C 7700.jpg -> 49-20-43-20-37-37-30-30-2e-4a-50-47

What does that mean? What stands "d" for?


Comment on Re^2: Read text file - Encoding problem?
Replies are listed 'Best First'.
Re^3: Read text file - Encoding problem?
by better (Acolyte) on Mar 17, 2013 at 12:18 UTC

    In finding out, how to remove this "d", which is invisibly attached at the end of each line, I included into your script:

    chop $entry

    chomp wouldn't do!

    The gethex function shows that "d" is removed without loosing the last letter (or number).

    Later I will continue working on the question, how to parse the "bad" text file without the invisible "d" into my main script and use these shortened strings there as regex


        Thanks for this link!

        Is it correct, that this Carriage return (cr) is obviously not removed by chomp? And that this could be the reason, why the regex (including cr) can not be matched to the array elements of read_dir (which have no cr)?

        Well, I will try to remove cr by defining $/ = "\r\n" and I will see if the script is running


        ...and later:

        It works!!!

        After parsing the csv file and writing the entries of the first coloumn into a text file, stripping of the carriage return. My main script "search and copy files" works.

        Thanks to all monks, who helped me to find enlightenment ;-)

        I'm feeling much better now

        And here is the code:

        #! /usr/local/bin/perl # #script opens and parses CSV files #removes carriage return #and writes all into a new text file # #tested: --ok! use strict; use warnings; use Text::CSV; #use Encode; # probably not needed :encoding(utf8) #Input CSV filename my $file = $ARGV[0]; if (!$ARGV[0]) { $file = './data/IDs.csv'; #Default } if (!-f $file) { print "Kann Datei $file nicht finden: $!\n"; exit 1; } #Parsing CSV local $/ = "\r\n"; #add windows carriage return to perl's eol +(newline) my $csv = Text::CSV ->new ({binary =>1, eol => $/}); open (my $fhCSV, '<', $file) or die "Kann $file nicht öffnen $!\n"; open (my $fhOUT, '>', './data/IDs.txt') or die "Kann Datei nicht öffn +en: $!\n"; while (my $line = <$fhCSV>) { if ($csv->parse($line)) { my @fields = $csv->fields (); chomp (@fields); print $fhOUT "@fields\n"; } else { warn "Line could not be parsed: $line\n"; } } print "CSV parsed and saved as text file: /data/IDs.txt!"; close $fhCSV; close $fhOUT;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1023899]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (5)
As of 2015-10-07 21:28 GMT
Find Nodes?
    Voting Booth?

    Does Humor Belong in Programming?

    Results (200 votes), past polls