Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Converting CSV files to fixed width flat files

by jkinchlow (Initiate)
on May 27, 2003 at 19:40 UTC ( #261093=perlquestion: print w/ replies, xml ) Need Help??
jkinchlow has asked for the wisdom of the Perl Monks concerning the following question:

I need help converting a csv file into a fixed width ascii flat file on the Win2000 os. Anyone have suggestions or ideas ? thanks Signed Overworked IT Guy and a perl beginner

Comment on Converting CSV files to fixed width flat files
Re: Converting CSV files to fixed width flat files
by sschneid (Deacon) on May 27, 2003 at 19:43 UTC
    Check out split to separate the CSV file into an array, and use printf to print it out fixed-width.

    perldoc -f <function> if you need help with syntax, etc.

    -s.
Re: Converting CSV files to fixed width flat files
by halley (Prior) on May 27, 2003 at 19:48 UTC

    See Text::CSV. Works well to read or write CSV (you usually can't just split on commas).

    The perl builtin sprintf should be just fine for building fixed-width fields, or printf to build and print in one step.

    --
    [ e d @ h a l l e y . c c ]

      halley,
      Just to extend your very correct comment a little further:
      #!/usr/bin/perl -w use strict; use Text::CSV; my $csv = Text::CSV->new; open (INPUT,"input.txt") or die "\nCan't open input.txt for reading : +$!\n"; open (OUTPUT,">output.txt") or die "\nCan't open output.txt for writin +g : $!\n"; select OUTPUT; while (<INPUT>) { $csv->parse($_); my @IF = $csv->fields(); foreach my $element (@IF) { #printf command goes here } print "\n"; }
      Of course printf may truncate fields that are too short long and there are more programmer efficient ways to do the printf. I was just using this syntax for readability purposes.

      Cheers - L~R

        To the best of my knowledge, printf (and sprintf) will never truncate a field that is too long for the width specified in the format string -- it will always provide each field with as many characters as needed to fit a given string with "%s" or all digits with "%d", or all digits to the left of the decimal point with "%f", thereby screwing up the programmer's intended vertical alignment of fields on successive lines, rather than eliminate significant data that would not have fit the programmer's margins.

        update: thanks to PodMaster for some much needed tutoring... my comments above did not consider format specs that really do impose field truncation, like "%1.1s" -- I was only thinking of the more common flavor, like "%1s", which does not truncate. (I still don't know how to truncate numerics...)

Re: Converting CSV files to fixed width flat files
by sauoq (Abbot) on May 27, 2003 at 19:54 UTC

    The Text::CSV module can help you parse the file. How to convert it might be pretty dependent on what you actually want to do and what you know about the data. Do you need to determine maximum field widths from the data? How do you want to pad short fields? Etc. etc.

    If you need more help, an example might be in order.

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: Converting CSV files to fixed width flat files
by Anonymous Monk on May 27, 2003 at 20:19 UTC
    EXAMPLE I need to convert the csv file into a fixed width file with a data dictionary. The data dictionary must specify the length of each field but I do not know the maximum size on the inbound csv file. I also need to assign a type but I am ok with changing that manually once I have a reasonable data dictionary. I attempted to play with excel but it fell over from the file depth. Thanks!!! for your help!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://261093]
Approved by halley
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (10)
As of 2014-09-30 11:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (363 votes), past polls