Best way to read a csv file?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dmorand
    New Member
    • Sep 2007
    • 219

    Best way to read a csv file?

    I've got a csv file that I need to reformat using a perl script. I need to take the 14th and 16th positions and combine them together into one field with some characters. I'm still new to perl so I'm not sure the best practice. I was going to use a do loop and to get to the 14th "," value. Then read the chars up to the next ",". Skip the next comma value, then do the same for position 16.

    I'm assuming there's a better way to do this.
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    Originally posted by dmorand
    I've got a csv file that I need to reformat using a perl script. I need to take the 14th and 16th positions and combine them together into one field with some characters. I'm still new to perl so I'm not sure the best practice. I was going to use a do loop and to get to the 14th "," value. Then read the chars up to the next ",". Skip the next comma value, then do the same for position 16.

    I'm assuming there's a better way to do this.
    This being Perl, there is always TIMTOWTDI. That said, you could always use the DBI interface and include the DBD::CSV module which treats a CSV file as a database, allowing you easier access to each entry. I haven't used it, but read about it.

    Regards,

    Jeff

    Comment

    • KevinADC
      Recognized Expert Specialist
      • Jan 2007
      • 4092

      #3
      look into Text::CSV or Text::CSV_XS

      You can find them on CPAN as well as other repositories:

      Comment

      • dmorand
        New Member
        • Sep 2007
        • 219

        #4
        Cool thanks. I'll check that out.

        Comment

        • KevinADC
          Recognized Expert Specialist
          • Jan 2007
          • 4092

          #5
          Originally posted by numberwhun
          This being Perl, there is always TIMTOWTDI. That said, you could always use the DBI interface and include the DBD::CSV module which treats a CSV file as a database, allowing you easier access to each entry. I haven't used it, but read about it.

          Regards,

          Jeff
          If a person is already familiar with SQL or their CSV file is structure as such, that is an option, interestingly, DBD::CSV module is a wrapper for Text::CSV_XS.

          Comment

          • numberwhun
            Recognized Expert Moderator Specialist
            • May 2007
            • 3467

            #6
            Originally posted by KevinADC
            If a person is already familiar with SQL or their CSV file is structure as such, that is an option, interestingly, DBD::CSV module is a wrapper for Text::CSV_XS.
            Oh, ok. Thanks! Wasn't aware.

            Comment

            Working...