intermediate python csv reader/writer question from a beginner

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • labmice
    New Member
    • Feb 2009
    • 1

    #1

    intermediate python csv reader/writer question from a beginner

    anything related to csv, I usually use VB within excel to manipulate
    the data, nonetheless, i finally got the courage to take a dive into
    python. i have viewed a lot of googled csv tutorials, but none of
    them address everything i need. Nonetheless, I was wondering if
    someone can help me manipulate the sample csv (sample.csv) I have
    generated:
    Code:
    ,,
    someinfo,,,,,,,
    somotherinfo,,,,,,,
    SEQ,Names,Test1,Test2,Date,Time,,
    1,Adam,1,2,Monday,1:00 PM,,
    2,Bob,3,4,Monday,1:00 PM,,
    3,Charlie,5,6,Monday,1:00 PM,,
    4,Adam,7,8,Monday,2:00 PM,,
    5,Bob,9,10,Monday,2:00 PM,,
    6,Charlie,11,12,Monday,2:00 PM,,
    7,Adam,13,14,Tuesday,1:00 PM,,
    8,Bob,15,16,Tuesday,1:00 PM,,
    9,Charlie,17,18,Tuesday,1:00 PM,,
    into (newfile.csv):
    Code:
    Adam-Test1,Adam-Test2,Bob-Test1,Bob-Test2,Charlie-Test1,Charlie-
    Test2,Date,Time
    1,2,3,4,5,6,Monday,1:00 PM
    7,8,9,10,11,12,Monday,2:00 PM
    13,14,15,16,17,18,Tuesday,1:00 PM
    note:
    1. the true header doesn't start line 4 (if this is the case would i
    have to use "split"?)
    2. if there were SEQ#10-12, or 13-15, it would still be Adam, Bob,
    Charlie, but with different Test1/Test2/Date/Time
    Last edited by pbmods; Feb 24 '09, 03:15 AM. Reason: Added CODE tags.
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    The key is to get the data into a dictionary that can be manipulated for your purpose. Note that the following code keeps the data in order:
    Code:
    import csv
    labels = ['SEQ','Names','Test1','Test2','Date','Time']
    f = open(r"csv1.txt")
    
    n = 4
    # skip first 4 lines
    for i in range(4):
        f.next()
    
    reader = csv.DictReader(f, labels)
    readerDict = {}
    
    for item in reader:
        for key in item:
            readerDict.setdefault(key, []).append(item[key])
    
    f.close()
    
    for key in labels:
        print key, readerDict[key]
    Output:
    Code:
    >>> SEQ ['1', '2', '3', '4', '5', '6', '7', '8', '9']
    Names ['Adam', 'Bob', 'Charlie', 'Adam', 'Bob', 'Charlie', 'Adam', 'Bob', 'Charlie']
    Test1 ['1', '3', '5', '7', '9', '11', '13', '15', '17']
    Test2 ['2', '4', '6', '8', '10', '12', '14', '16', '18']
    Date ['Monday', 'Monday', 'Monday', 'Monday', 'Monday', 'Monday', 'Tuesday', 'Tuesday', 'Tuesday']
    Time ['1:00 PM', '1:00 PM', '1:00 PM', '2:00 PM', '2:00 PM', '2:00 PM', '1:00 PM', '1:00 PM', '1:00 PM']
    >>>
    Last edited by bvdet; Feb 24 '09, 01:58 AM. Reason: close open file object

    Comment

    Working...