SHARE
TWEET

Untitled

a guest Jun 25th, 2019 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. my csv file:
  2. id;Remarks;x;y;z
  3. a1;Mv_biw;10;12;3
  4. b1;Ins_slt_po_zd;20;22;5
  5. a2;Mv_biw;25;17;7
  6. a1;Ins_slt_po_zd;35;13;3
  7. a1;Ins_slt_dkz;15;19;9
  8. b1;Mv_biw;65;11;2
  9. b2;Ins_slt_dkz;50;23;1
  10. b2;Mv_biw;75;17;7
  11.  
  12. my code sofar:
  13. import os
  14. import csv
  15. import collections
  16. from csv import DictWriter    
  17.  
  18. with open(r'C:UsersrphDesktopkar_csv_testworkfilesinternetnewincsv_new.csv', 'r') as csvfile:
  19.     reader = csv.reader(csvfile, delimiter=';')
  20.     my_dict = collections.defaultdict(dict)
  21.     for row in reader:
  22.         my_dict[row[0]][row[1]] = [row[2],row[3],row[4]]
  23.  
  24. print (my_dict)
  25.  
  26.  
  27. with open(r'C:UsersrphDesktopkar_csv_testworkfilesinternetnewoutcsv_new.csv','w', newline='') as wf:
  28.     fieldnames = ['ID','x_Mv_biw', 'y_Mv_biw', 'z_Mv_biw', 'x_Ins_slt_po_zd', 'y_Ins_slt_po_zd', 'z_Ins_slt_po_zd', 'x_Slb_po_zd', 'y_Slb_po_zd', 'z_Slb_po_zd', 'x_Slb_dkz', 'y_Slb_dkz', 'z_Slb_dkz', 'x_Ins_slt_dkz', 'y_Ins_slt_dkz', 'z_Ins_slt_dkz']
  29.     my_write = csv.DictWriter(wf, fieldnames = fieldnames, delimiter = ';')
  30.     my_write.writeheader()
  31.  
  32. desired output csvfile:
  33. id;x_Mv_biw;y_Mv_biw;z_Mv_biw;x_Ins_slt_po_zd;y_Ins_slt_po_zd;z_Ins_slt_po_zd;z_Ins_slt_po_zd;x_Slb_po_zd;y_Slb_po_zd;z_Slb_po_zd;x_Slb_dkz;y_Slb_dkz;z_Slb_dkz;x_Ins_slt_dkz;y_Ins_slt_dkz;z_Ins_slt_dkz
  34. a1;10;12;3;35;13;3;;;;;;;;15;19;9
  35. a2;25;17;7;;;;;;;;;;;;;
  36. b1;65;11;2;20;22;5;;;;;;;;;;
  37. b2;75;17;7;;;;;;;;;;;50;23;1
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top