Guest User

Untitled

a guest
Feb 17th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. lists, fruits, books, forks, rope, gum
  2. 4, 2, 3, 0, 2, 2
  3.  
  4. Manhattan Produce Market
  5. id, fruit, color
  6. 1, orange, orange
  7. 2, apple, red
  8.  
  9. Books
  10. id, book, pages
  11. 1, Webster’s Dictionary, 1000
  12. 2, Tony the Towtruck, 20
  13. 3, The Twelfth Night, 144
  14.  
  15. Rope
  16. id, rope, length, diameter, color
  17. 1, hemp, 12-feet, .5, green
  18. 2, sisal, 50-feet, .125, brown
  19.  
  20. Kings County Candy
  21. id, flavor, color, big-league
  22. 1, grape, purple, yes
  23. 2, mega mango, yellow-orange, no
  24.  
  25. import csv, re
  26. header = []
  27. table = []
  28.  
  29. with open('toy.csv', 'r') as blob:
  30. reader = csv.reader(blob)
  31. curr = reader.__next__()
  32. while True:
  33. prev = curr
  34. try:
  35. curr = reader.__next__()
  36. except StopIteration:
  37. break
  38. if not ['id', ' book', ' pages'] == curr:
  39. continue
  40. else:
  41. header.append(prev)
  42. table.append(['title'] + curr)
  43. while True:
  44. try:
  45. curr = reader.__next__()
  46. if curr == []:
  47. break
  48. else:
  49. table.append(header[0] + curr)
  50. except StopIteration:
  51. break
  52.  
  53. [['title', 'id', ' book', ' pages'],
  54. ['Books', '1', ' Webster’s Dictionary', ' 1000'],
  55. ['Books', '2', ' Tony the Towtruck', ' 20'],
  56. ['Books', '3', ' The Twelfth Night', ' 144']]
Add Comment
Please, Sign In to add comment