PYCON 22
ParseResults2R.py Guest on 24th October 2020 07:31:05 AM
  1. import csv
  2. import itertools
  3. import random
  4. import ast
  5.  
  6. # put files in the order you want concatentated
  7. csv_names = ["qud_pilot.results","qud_pilot2.results"]
  8. #csv_names = ["qud_pilot.results"]
  9.  
  10. lines = []
  11. headers = []
  12.  
  13. readers = [csv.DictReader(open(fn, 'rb'),delimiter="\t",quotechar='\"') for fn in csv_names]
  14.  
  15. for r in readers:
  16.         lines.extend(list(r))
  17.         headers = r.fieldnames
  18.        
  19. print headers  
  20. print lines[0] 
  21.  
  22. for l in lines:
  23.         triallist = ast.literal_eval(l['Answer.trialInfo'])
  24.         for k in range(1,3): # 2 quds
  25.                 l['QUD'+str(k)] = triallist[k-1]['qud']
  26.                 l['Response'+str(k)] = triallist[k-1]['response']              
  27.                 l['Speaker1'+str(k)] = triallist[k-1]['speaker1']
  28.                 l['Speaker2'+str(k)] = triallist[k-1]['speaker2']                              
  29.                 l['TargetColor'+str(k)] = triallist[k-1]['targetcolor']        
  30.         del l['Answer.trialInfo']
  31.  
  32.  
  33. headers.remove('Answer.trialInfo')
  34. headers = headers+["QUD"+str(i) for i in range(1,3)]
  35. headers = headers+["Response"+str(i) for i in range(1,3)]
  36. headers = headers+["Speaker1"+str(i) for i in range(1,3)]
  37. headers = headers+["Speaker2"+str(i) for i in range(1,3)]
  38. headers = headers+["TargetColor"+str(i) for i in range(1,3)]
  39.        
  40. w = csv.DictWriter(open('result.csv', 'wb'),fieldnames=headers,restval="NA",delimiter="\t")
  41. w.writeheader()
  42. w.writerows(lines)

Paste is for source code and general debugging text.

Login or Register to edit, delete and keep track of your pastes and more.

Recent Pastes

Raw Paste

Login or Register to edit or fork this paste. It's free.