CSV to quasi-XML

Discussion in 'Perl Misc' started by Travis, Apr 14, 2008.

  1. Travis

    Travis Guest

    Hi there,

    Never written a line of Perl until now but I think it might be perfect
    for this little program I need.

    I have some CSV files that are given to me and I need to spit them in
    a format that kind of looks like XML but I don't think it is. The
    structure looks something like this:

    <ADDRESSES>
    <struct>
    <field name="First" value="John" />
    <field name="Last" value="Doe" />
    <field name="City" value="San Francisco" />
    </struct><struct>
    <field name="First" value="Jane" />
    <field name="Last" value="Johnson" />
    <field name="City" value="New York City" />
    </struct>
    </ADDRESSES>


    I don't think that's standard XML is it? If so please let me know so I
    can change my google searching on the topic.

    Anyway, I get CSV's that look something like this:

    First,Last,City
    John,Doe,San Francisco
    Jane,Johnson,New York City


    I'm a C/C++ person by nature but I thought this would be a good way to
    get my hands in Perl and learn a bit.

    Any guidance is helpful. Thanks.
    Travis, Apr 14, 2008
    #1
    1. Advertising

  2. Travis

    Travis Guest

    Oh also there should be a newline \n between the closing </struct> and
    the next <struct>
    Travis, Apr 14, 2008
    #2
    1. Advertising

  3. Travis <> wrote in
    news:1db6609e-07f3-4388-befb-9088405d95e9
    @w4g2000prd.googlegroups.com
    :

    > Never written a line of Perl until now but I think it might be
    > perfect for this little program I need.


    It looks you are planning to have someone else write the lines of
    Perl that might be needed rather than writing them yourself.

    > I have some CSV files that are given to me and I need to spit them
    > in a format that kind of looks like XML but I don't think it is.
    > The structure looks something like this:
    >
    > <ADDRESSES>
    > <struct>
    > <field name="First" value="John" />
    > <field name="Last" value="Doe" />
    > <field name="City" value="San Francisco" />
    > </struct><struct>
    > <field name="First" value="Jane" />
    > <field name="Last" value="Johnson" />
    > <field name="City" value="New York City" />
    > </struct>
    > </ADDRESSES>
    >
    >
    > I don't think that's standard XML is it?


    Of course, it is.

    > If so please let me know
    > so I can change my google searching on the topic.


    Why would searching Google be easier than writing the program in the
    first place?

    >
    > Anyway, I get CSV's that look something like this:
    >
    > First,Last,City
    > John,Doe,San Francisco
    > Jane,Johnson,New York City
    >
    > I'm a C/C++ person by nature


    No one is a 'C/C++ person' by nature. If you know C, this is trivial
    to write. So, first write the program in C, then read
    <URL:http://www.ebb.org/PickingUpPerl/>. You can then come back and
    ask if you run into any problems.

    > but I thought this would be a good
    > way to get my hands in Perl and learn a bit.
    >
    > Any guidance is helpful. Thanks.


    OK, here is a part of a fish:

    my ($first, $last, $city) = split /,/, $line;

    print <<STRUCT;
    <struct>
    <field name="First" value="$first" />
    <field name="Last" value="$last" />
    <field name="City" value="$city" />
    </struct>
    STRUCT

    There is also:

    http://csv2xml.sourceforge.net/

    Sinan

    --
    A. Sinan Unur <>
    (remove .invalid and reverse each component for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://www.rehabitation.com/clpmisc/
    A. Sinan Unur, Apr 14, 2008
    #3
  4. Travis <> wrote in
    news:
    m:

    > Oh also there should be a newline \n between the closing </struct>
    > and the next <struct>


    I knew I was making a mistake replying. *Sigh*. Just confirmed my
    expectations.

    Sinan

    --
    A. Sinan Unur <>
    (remove .invalid and reverse each component for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://www.rehabitation.com/clpmisc/
    A. Sinan Unur, Apr 14, 2008
    #4
  5. Travis

    Travis Guest

    On Apr 14, 2:52 pm, "A. Sinan Unur" <> wrote:
    > Travis <> wrote in
    > news:1db6609e-07f3-4388-befb-9088405d95e9
    > @w4g2000prd.googlegroups.com
    > :
    >
    > > Never written a line of Perl until now but I think it might be
    > > perfect for this little program I need.

    >
    > It looks you are planning to have someone else write the lines of
    > Perl that might be needed rather than writing them yourself.
    >
    >
    >
    > > I have some CSV files that are given to me and I need to spit them
    > > in a format that kind of looks like XML but I don't think it is.
    > > The structure looks something like this:

    >
    > > <ADDRESSES>
    > > <struct>
    > >     <field name="First" value="John" />
    > >     <field name="Last" value="Doe" />
    > >     <field name="City" value="San Francisco" />
    > > </struct><struct>
    > >     <field name="First" value="Jane" />
    > >     <field name="Last" value="Johnson" />
    > >     <field name="City" value="New York City" />
    > > </struct>
    > > </ADDRESSES>

    >
    > > I don't think that's standard XML is it?

    >
    > Of course, it is.
    >
    > > If so please let me know
    > > so I can change my google searching on the topic.

    >
    > Why would searching Google be easier than writing the program in the
    > first place?
    >
    >
    >
    > > Anyway, I get CSV's that look something like this:

    >
    > > First,Last,City
    > > John,Doe,San Francisco
    > > Jane,Johnson,New York City

    >
    > > I'm a C/C++ person by nature

    >
    > No one is a 'C/C++ person' by nature. If you know C, this is trivial
    > to write. So, first write the program in C, then read
    > <URL:http://www.ebb.org/PickingUpPerl/>. You can then come back and
    > ask if you run into any problems.
    >
    > > but I thought this would be a good
    > > way to get my hands in Perl and learn a bit.

    >
    > > Any guidance is helpful. Thanks.

    >
    > OK, here is a part of a fish:
    >
    > my ($first, $last, $city) = split /,/, $line;
    >
    > print <<STRUCT;
    > <struct>
    >     <field name="First" value="$first" />
    >     <field name="Last" value="$last" />
    >     <field name="City" value="$city" />
    > </struct>
    > STRUCT
    >
    > There is also:
    >
    > http://csv2xml.sourceforge.net/
    >
    > Sinan
    >
    > --
    > A. Sinan Unur <>
    > (remove .invalid and reverse each component for email address)
    >
    > comp.lang.perl.misc guidelines on the WWW:http://www.rehabitation.com/clpmisc/


    lol thanks for the taste. i appreciate it.
    Travis, Apr 14, 2008
    #5
  6. Travis

    ccc31807 Guest

    On Apr 14, 5:30 pm, Travis <> wrote:
    > I have some CSV files that are given to me and I need to spit them in
    > a format that kind of looks like XML but I don't think it is.


    The pseudocode for your little problem looks like this:

    open infile for reading;
    open outfile for writing;
    while outfile is open;
    read each line;
    remove newline character;
    split on delimiter into data structure;
    join into string representation of output;
    print string to your outfile;
    close infile;
    close outfile;

    The functions you will need are open, close, while, chomp, split,
    join, print (or printf or sprintf), and possibly some data structure
    specific functions depending on your approach. Personally, I would use
    an array.

    > The
    > structure looks something like this:
    >
    > <ADDRESSES>
    > <struct>
    > <field name="First" value="John" />
    > <field name="Last" value="Doe" />
    > <field name="City" value="San Francisco" />
    > </struct><struct>
    > <field name="First" value="Jane" />
    > <field name="Last" value="Johnson" />
    > <field name="City" value="New York City" />
    > </struct>
    > </ADDRESSES>


    This is well formed XML. You might want to know what 'well formed'
    means.

    > I'm a C/C++ person by nature but I thought this would be a good way to
    > get my hands in Perl and learn a bit.


    Good. As you will discover, Perl is optimized to perform a pretty
    narrow range of tasks, but this is an ideal job for Perl. In my job
    (database manager) I use Perl for about 80% of everything I do, most
    of which is very similar to your problem. I have used C as well, but I
    don't get to do much bit twiddling.

    CC
    ccc31807, Apr 15, 2008
    #6
  7. Travis

    Travis Guest

    Thanks all for the wonderful help.
    Travis, Apr 16, 2008
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Clark Griswold

    Python: Good for Quasi-Newbie?

    Clark Griswold, Nov 23, 2003, in forum: Python
    Replies:
    2
    Views:
    358
    Lee Harr
    Nov 23, 2003
  2. Florian Weimer

    Quasi-standard library for byte arrays

    Florian Weimer, Oct 17, 2011, in forum: C Programming
    Replies:
    1
    Views:
    315
    jacob navia
    Oct 17, 2011
  3. Ilias Lazaridis
    Replies:
    9
    Views:
    112
    Michele Dondi
    Jun 23, 2007
  4. Dr J R Stockton

    JScript: - SEAKFYLE - 32-bit quasi-HUNT, quasi-DIR

    Dr J R Stockton, Dec 17, 2011, in forum: Javascript
    Replies:
    1
    Views:
    558
    Dr J R Stockton
    Dec 19, 2011
  5. David RF

    Quasi 0

    David RF, Jun 4, 2012, in forum: C Programming
    Replies:
    19
    Views:
    569
    Stefan Ram
    Jun 5, 2012
Loading...

Share This Page