quick and smart way for parsing a CSV file?

Discussion in 'Python' started by Hank, Sep 3, 2003.

  1. Hank

    Hank Guest

    Hi,

    I have a CSV file from excel that looks like this (simplified):

    name,description,type1,type2,name,filename
    test1,this is a test,0.000,1.000,,
    test2,another test,1.000,0.000,newname,filename
    test3,this is a test,0.000,1.000,,
    test4,this is a test,0.000,1.000,,
    test5,this is a test,0.000,1.000,,
    test6,this is a test,0.000,1.000,,



    what i want at the end is a dictionary like

    dict1[name] = test1
    dict1[description] = "this is a test"
    ..
    ..
    ..
    dict2[name] = test2
    dict2[description] = "another test"

    whats a fast and quick way of parsing it? Look for the first comma,
    strip that entry out of the string, keep going? Thought there might be
    some shortcuts.

    Thanks
     
    Hank, Sep 3, 2003
    #1
    1. Advertising

  2. soundwave56> I have a CSV file from excel that looks like this
    soundwave56> (simplified):

    soundwave56> name,description,type1,type2,name,filename
    soundwave56> test1,this is a test,0.000,1.000,,
    soundwave56> test2,another test,1.000,0.000,newname,filename
    soundwave56> test3,this is a test,0.000,1.000,,
    soundwave56> test4,this is a test,0.000,1.000,,
    soundwave56> test5,this is a test,0.000,1.000,,
    soundwave56> test6,this is a test,0.000,1.000,,

    Take a look at the csv module delivered with Python 2.3.

    Skip
     
    Skip Montanaro, Sep 3, 2003
    #2
    1. Advertising

  3. Hank

    Guest

    Hank wrote:
    >
    > Hi,
    >
    > I have a CSV file from excel that looks like this (simplified):
    >
    > name,description,type1,type2,name,filename
    > test1,this is a test,0.000,1.000,,
    > test2,another test,1.000,0.000,newname,filename
    > test3,this is a test,0.000,1.000,,
    > test4,this is a test,0.000,1.000,,
    > test5,this is a test,0.000,1.000,,
    > test6,this is a test,0.000,1.000,,
    >
    > what i want at the end is a dictionary like
    >
    > dict1[name] = test1
    > dict1[description] = "this is a test"
    > .
    > .
    > .
    > dict2[name] = test2
    > dict2[description] = "another test"
    >



    import csv

    dicts = []

    inputFile = open("SomeDurnFileName.csv", "rb")
    parser = csv.reader(inputFile)
    firstRec = True
    for fields in parser:
    if firstRec:
    fieldNames = fields
    firstRec = False
    else:
    dicts.append({})
    for i,f in enumerate(fields)
    dicts[-1][fieldNames] = f
     
    , Sep 3, 2003
    #3
  4. Hank

    John Hunter Guest

    >>>>> "Hank" == Hank <> writes:

    Hank> Hi, I have a CSV file from excel that looks like this
    Hank> (simplified):

    Hank> name,description,type1,type2,name,filename test1,this is a
    Hank> test,0.000,1.000,, test2,another
    Hank> test,1.000,0.000,newname,filename test3,this is a
    Hank> test,0.000,1.000,, test4,this is a test,0.000,1.000,,
    Hank> test5,this is a test,0.000,1.000,, test6,this is a
    Hank> test,0.000,1.000,,

    I posted some code recently to parse CSV into a dictionary where you
    can extract rows or columns by key or index number. It's not exactly
    what you asked for, but might help

    http://groups.google.com/groups?hl=&rnum=4

    John Hunter
     
    John Hunter, Sep 4, 2003
    #4
  5. <> schrieb im Newsbeitrag
    news:...
    > Hank wrote:
    > >
    > > Hi,
    > >
    > > I have a CSV file from excel that looks like this (simplified):
    > >

    ..........
    >
    >
    > import csv
    >
    > dicts = []
    >
    > inputFile = open("SomeDurnFileName.csv", "rb")
    > parser = csv.reader(inputFile)


    If you do not use Python 2.3 for some reason or other there is a third party
    CSV that is fast and workes fine with Python 2.2 (but uses a different
    interface of course)
    http://www.object-craft.com.au/projects/csv/

    Kindly
    Michael P
     
    Michael Peuser, Sep 4, 2003
    #5
  6. Hank

    Hank Guest

    where would i get this csv module from? does it come with python 2.2?

    thanks


    wrote in message news:<>...
    > Hank wrote:
    > >
    > > Hi,
    > >
    > > I have a CSV file from excel that looks like this (simplified):
    > >
    > > name,description,type1,type2,name,filename
    > > test1,this is a test,0.000,1.000,,
    > > test2,another test,1.000,0.000,newname,filename
    > > test3,this is a test,0.000,1.000,,
    > > test4,this is a test,0.000,1.000,,
    > > test5,this is a test,0.000,1.000,,
    > > test6,this is a test,0.000,1.000,,
    > >
    > > what i want at the end is a dictionary like
    > >
    > > dict1[name] = test1
    > > dict1[description] = "this is a test"
    > > .
    > > .
    > > .
    > > dict2[name] = test2
    > > dict2[description] = "another test"
    > >

    >
    >
    > import csv
    >
    > dicts = []
    >
    > inputFile = open("SomeDurnFileName.csv", "rb")
    > parser = csv.reader(inputFile)
    > firstRec = True
    > for fields in parser:
    > if firstRec:
    > fieldNames = fields
    > firstRec = False
    > else:
    > dicts.append({})
    > for i,f in enumerate(fields)
    > dicts[-1][fieldNames] = f
     
    Hank, Sep 4, 2003
    #6

  7. >> where would i get this csv module from? does it come with python 2.2?
    >> thanks


    It comes with Python 2.3. If you grab the csv and _csv modules from the
    Python 2.3 CVS repository, they should build and run under 2.2.x. If that's
    not possible, poke around for Object Craft's csv module.

    Skip
     
    Skip Montanaro, Sep 4, 2003
    #7
  8. "Hank" <> schrieb im Newsbeitrag
    news:...
    > where would i get this csv module from? does it come with python 2.2?
    >
    > thanks


    No of course - read my posting!

    Kindly
    Michael P
     
    Michael Peuser, Sep 4, 2003
    #8
  9. Hank

    Hank Guest

    I'm using ActivePython2.2 which has the win32 extensions included.
    Don't think they have a ActivePython2.3 out yet.

    Also I was trying out PAGE which needed 2.2. Don't have the resources
    to recompile for 2.3.

    Thanks for the help!

    "Michael Peuser" <> wrote in message news:<bj85d3$iav$03$-online.com>...
    > <> schrieb im Newsbeitrag
    > news:...
    > > Hank wrote:
    > > >
    > > > Hi,
    > > >
    > > > I have a CSV file from excel that looks like this (simplified):
    > > >

    > .........
    > >
    > >
    > > import csv
    > >
    > > dicts = []
    > >
    > > inputFile = open("SomeDurnFileName.csv", "rb")
    > > parser = csv.reader(inputFile)

    >
    > If you do not use Python 2.3 for some reason or other there is a third party
    > CSV that is fast and workes fine with Python 2.2 (but uses a different
    > interface of course)
    > http://www.object-craft.com.au/projects/csv/
    >
    > Kindly
    > Michael P
     
    Hank, Sep 5, 2003
    #9
    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. GIMME
    Replies:
    2
    Views:
    877
    GIMME
    Feb 11, 2004
  2. Tintin92
    Replies:
    1
    Views:
    1,715
    Andrew Thompson
    Feb 14, 2007
  3. MotoK
    Replies:
    59
    Views:
    1,818
    Keith Thompson
    Sep 15, 2006
  4. jliu66
    Replies:
    0
    Views:
    516
    jliu66
    Oct 19, 2007
  5. sso
    Replies:
    20
    Views:
    2,673
    Martin Gregorie
    Apr 26, 2009
Loading...

Share This Page