Advertisement
Guest User

Untitled

a guest
Apr 21st, 2014
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. one,two,three
  2. 1,2,3
  3. 2,3,4
  4. 3,4,5
  5.  
  6. ,two,three
  7. ,2,3
  8. ,3,4
  9. ,4,5
  10.  
  11. import csv
  12.  
  13. with open('code.csv', 'rb') as input, open('result.csv', 'wb') as output:
  14. reader = csv.DictReader(input)
  15. rows = [row for row in reader]
  16. writer = csv.writer(output, delimiter = ',')
  17.  
  18. writer.writerow(["new_one", "new_two", "new_three"])
  19.  
  20. for row in rows:
  21. if 'two' in row:
  22. writer.writerow(['',row["two"]])
  23.  
  24. for row in rows:
  25. if 'one' in row:
  26. writer.writerow([row["one"]])
  27.  
  28. new_one,new_two,new_three
  29. ,2
  30. ,3
  31. ,4
  32. 1
  33. 2
  34. 3
  35.  
  36. for row in rows:
  37. if 'one' in row:
  38. writer.writerow([row["one"]])
  39.  
  40. new_one,new_two,new_three
  41. 1,2
  42. 2,3
  43. 3,4
  44.  
  45. for row in rows:
  46. if 'one' in row and 'two' in row:
  47. writer.writerow([row["one"],row["two"]])
  48.  
  49. for row in rows:
  50. writer.writerow([row.get("one", ""), row.get("two", "")])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement