Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^2: Textfile to csv with a small twist

by jZed (Prior)
on Aug 25, 2005 at 18:47 UTC ( [id://486686]=note: print w/replies, xml ) Need Help??


in reply to Re: Textfile to csv with a small twist
in thread Textfile to csv with a small twist

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!
  • Comment on Re^2: Textfile to csv with a small twist

Replies are listed 'Best First'.
Re^3: Textfile to csv with a small twist
by pbeckingham (Parson) on Aug 25, 2005 at 19:12 UTC

    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.

        The data is a simple sample.
        No it isn't, it has embedded newlines.
        jZed provided no solution to the task at hand
        That's because I was waiting for the OP to clarify what he/she really wanted. Now that the OP has done that, I've given the example, please see my code.
Re^3: Textfile to csv with a small twist
by Bentov (Novice) on Aug 25, 2005 at 19:25 UTC
    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).
        Closer to the first one, what I'm exactly looking for is
        H1,T1.1 CRLF T1.2 crlf,H2,T2.1 CRLF T2.2 CRLF
        While the headings are supposed to be same in all of my files, I.e. they will always appear in the same order, and all of the headings will appear, I know they arn't. So I'm figuring that I'll need to have the heading as part of the record, that way if for example I have something like
        H1: T1.1 T1.2 H3: T3.1 T3.2 T3.3
        I can generate something like:
        H1,T1.1 CRLF T1.2 CRLF,H3,T3.1 CRLF T3.2 CRLF T3.3 CRLF
        and from that generate...
        H1,T1.1 CRLF T1.2 CRLF,H2,,H3,T3.1 CRLF T3.2 CRLF T3.3 CRLF
        Since the H2 heading didn't appear in the data, I will have to force in in the final file. I planned this to be a two steep process. I hope that explains things better. Bentov

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2024-04-24 09:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found