Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Textfile to csv with a small twist

by ChrisR (Hermit)
on Aug 25, 2005 at 18:42 UTC ( [id://486685]=note: print w/replies, xml ) Need Help??


in reply to Textfile to csv with a small twist

If I understood your post correctly, here's one way:
#!c:\perl\bin\perl -w use strict; my $currentheading; my %hash; my @headings; my @array; open(FILE,"c:\\test.txt"); while(my $line = <FILE>) { chomp($line); if($line =~ /(.*?:)$/) { $currentheading = $1; push @headings, $currentheading; } else { push @{$hash{$currentheading}}, $line; } } for my $x(0..$#headings) { my $record = 0; for my $y(0..$#{$hash{$headings[$x]}}) { $array[$record][$x] = $hash{$headings[$x]}[$y]; $record++; } } my $header = join ",",@headings; print "$header\n"; for my $x(0..$#array) { my $recordline = join ",", @{$array[$x]}; print "$recordline\n"; }
This will handle missing fields in certain records. Perl will issue a warning however if a field is missing.

Note: I removed the newlines to show the data in an easily readable format. To keep them just remove the line: chomp($line);

Update:looks like pbeckingham beat me to a similar solution.

Replies are listed 'Best First'.
Re^2: Textfile to csv with a small twist
by jZed (Prior) on Aug 25, 2005 at 18:47 UTC
    You make the same mistake as pbeckingham - CSV seems like a simple join with commas, but that only works for very simple CSV. If there are embedded commas, quote marks, or newlines, the join will produce garbage. Use a CSV parsing module!

      The data provided is clearly *sample* data, and the code provided is of the same nature. The OP is asking about how to approach this problem. You're complaining that complete, robust solutions are not being provided, and that's where I think you are missing the point.



      pbeckingham - typist, perishable vertebrate.
        Oh, sorry, I thought one of the points of answering "textfile to CSV" might be how to produce valid (in the sense of parseable) CSV.
        I'm with you.

        The data is a simple sample. The code provided is also a simple sample. The OP did not ask for a production quality solution nor was one given.

        On the other hand, jZed provided no solution to the task at hand. I haven't yet needed to use a CSV parsing module but would love to see a good example provided here.

      Wow! I appreciate all of the input to my problem. InfiniteSilence's output is the closest to what I'm looking for(I haven't looked at the output from all of the example yet); however seeing the varied replies, I see I didn't explain myself clearly enough. I am basically looking for output like his/hers, except w/o the commas, and still have the crlfs in there. I belive I can modify the code provided to suit my needs, but what do I know? My perl knowledge only fills a matchbook :( Bentov
        I am still not understanding what output you want. If your data is this:
        H1:
        T1.1
        T1.2
        H2:
        T2.1
        T2.2
        
        Do you want this:
        H1,H2
        "T1.1\nT1.2\n","T2.1\nT2.2\n"
        
        Or this:
        H1,H2
        "T1.1\n","T2.1\n"
        "T1.2\n","T2.2\n"
        
        Or something else? And another ambiguity: you haven't mentioned whether headings can repeat in your input data (for example more than one section labeled heading1).

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-25 17:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found