csv to list

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • newmaid
    New Member
    • Feb 2008
    • 5

    csv to list

    hi all..

    if i had a csv file that had data in the format:

    1, male, 1997
    2, male, 2000
    3, female , 1977
    etc

    how would i pull all 3 columns out of the csv into lists

    thanks for your help
  • jlm699
    Contributor
    • Jul 2007
    • 314

    #2
    Originally posted by newmaid
    hi all..

    if i had a csv file that had data in the format:

    1, male, 1997
    2, male, 2000
    3, female , 1977
    etc

    how would i pull all 3 columns out of the csv into lists

    thanks for your help
    [CODE=python]
    >>> list = []
    >>> fh = open('test.csv' , 'r')
    >>>
    >>> for line in fh:
    >>> list.append(lin e.strip().split (','))
    >>>
    >>> list
    [['1', ' male', '1997'], ['2', 'male', '2000'], ['3', 'femail', '1977']]
    [/CODE]

    I'm not quite sure how you want your output data structured... but this is a guess.

    Comment

    • subeen
      New Member
      • Aug 2007
      • 8

      #3
      You can also use the csv module. Look at the following link:
      Source code: Lib/csv.py The so-called CSV (Comma Separated Values) format is the most common import and export format for spreadsheets and databases. CSV format was used for many years prior to att...


      regards,
      Subeen

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Originally posted by jlm699
        [CODE=python]
        >>> list = []
        >>> fh = open('test.csv' , 'r')
        >>>
        >>> for line in fh:
        >>> list.append(lin e.strip().split (','))
        >>>
        >>> list
        [['1', ' male', '1997'], ['2', 'male', '2000'], ['3', 'femail', '1977']]
        [/CODE]

        I'm not quite sure how you want your output data structured... but this is a guess.
        Good post jlm699 with one exception - using list for a variable name masks the built-in function list(). The following does basically the same as yours, but uses a list comprehension.
        [code=Python]
        data = '''1, male, 1997
        2, male, 2000
        3, female , 1977
        '''
        # data = open(file_name) .read()
        # list comprehension
        dataList = [line.split(',') for line in data.split('\n' ) if line][/code]In case the OP wants to transpose the rows and columns:
        [code=Python]def transpose_lists (dataList):
        # list comprehension
        return [[item[j] for item in dataList] for j in range(len(dataL ist[0]))]

        def transpose_lists (dataList):
        resultList = []
        for j in range(len(dataL ist[0])):
        temList = []
        for item in dataList:
        temList.append( item[j])
        resultList.appe nd(temList)
        return resultList[/code]

        Example:
        >>> dataList
        [['1', ' male', ' 1997'], ['2', ' male', ' 2000'], ['3', ' female ', ' 1977']]
        >>> transpose_lists (dataList)
        [['1', '2', '3'], [' male', ' male', ' female '], [' 1997', ' 2000', ' 1977']]
        >>>

        Comment

        • jlm699
          Contributor
          • Jul 2007
          • 314

          #5
          Originally posted by bvdet
          Good post jlm699 with one exception - using list for a variable name masks the built-in function list().
          lol, I tend to do that ALOT when I'm coming up with quick little examples... bit of a ditz I guess...

          That's a really neat little function there for transposing the data.. Nice!

          Comment

          • bvdet
            Recognized Expert Specialist
            • Oct 2006
            • 2851

            #6
            Originally posted by jlm699
            lol, I tend to do that ALOT when I'm coming up with quick little examples... bit of a ditz I guess...

            That's a really neat little function there for transposing the data.. Nice!
            Thanks jlm699. That's happened to many people including myself.

            Comment

            Working...