By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,001 Members | 1,176 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,001 IT Pros & Developers. It's quick & easy.

intermediate python csv reader/writer question from a beginner

P: 1
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:
Expand|Select|Wrap|Line Numbers
  1. ,,
  2. someinfo,,,,,,,
  3. somotherinfo,,,,,,,
  4. SEQ,Names,Test1,Test2,Date,Time,,
  5. 1,Adam,1,2,Monday,1:00 PM,,
  6. 2,Bob,3,4,Monday,1:00 PM,,
  7. 3,Charlie,5,6,Monday,1:00 PM,,
  8. 4,Adam,7,8,Monday,2:00 PM,,
  9. 5,Bob,9,10,Monday,2:00 PM,,
  10. 6,Charlie,11,12,Monday,2:00 PM,,
  11. 7,Adam,13,14,Tuesday,1:00 PM,,
  12. 8,Bob,15,16,Tuesday,1:00 PM,,
  13. 9,Charlie,17,18,Tuesday,1:00 PM,,
  14.  
into (newfile.csv):
Expand|Select|Wrap|Line Numbers
  1. Adam-Test1,Adam-Test2,Bob-Test1,Bob-Test2,Charlie-Test1,Charlie-
  2. Test2,Date,Time
  3. 1,2,3,4,5,6,Monday,1:00 PM
  4. 7,8,9,10,11,12,Monday,2:00 PM
  5. 13,14,15,16,17,18,Tuesday,1:00 PM
  6.  
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
Feb 23 '09 #1
Share this Question
Share on Google+
1 Reply


bvdet
Expert Mod 2.5K+
P: 2,851
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:
Expand|Select|Wrap|Line Numbers
  1. import csv
  2. labels = ['SEQ','Names','Test1','Test2','Date','Time']
  3. f = open(r"csv1.txt")
  4.  
  5. n = 4
  6. # skip first 4 lines
  7. for i in range(4):
  8.     f.next()
  9.  
  10. reader = csv.DictReader(f, labels)
  11. readerDict = {}
  12.  
  13. for item in reader:
  14.     for key in item:
  15.         readerDict.setdefault(key, []).append(item[key])
  16.  
  17. f.close()
  18.  
  19. for key in labels:
  20.     print key, readerDict[key]
Output:
Expand|Select|Wrap|Line Numbers
  1. >>> SEQ ['1', '2', '3', '4', '5', '6', '7', '8', '9']
  2. Names ['Adam', 'Bob', 'Charlie', 'Adam', 'Bob', 'Charlie', 'Adam', 'Bob', 'Charlie']
  3. Test1 ['1', '3', '5', '7', '9', '11', '13', '15', '17']
  4. Test2 ['2', '4', '6', '8', '10', '12', '14', '16', '18']
  5. Date ['Monday', 'Monday', 'Monday', 'Monday', 'Monday', 'Monday', 'Tuesday', 'Tuesday', 'Tuesday']
  6. 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']
  7. >>> 
Feb 24 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.