Guest User

Untitled

a guest
Oct 27th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. with open("input2.csv", 'r',encoding="utf8") as csvfile:
  2. # creating a csv reader object
  3. reader = csv.DictReader(csvfile, delimiter=',')
  4.  
  5. '''We then restructure the data to be a set of keys with list of values {key_1: [], key_2: []}:'''
  6. data = {}
  7. for row in reader:
  8. # print(row)
  9. for header, value in row.items():
  10. try:
  11. data[header].append(value)
  12. except KeyError:
  13. data[header] = [value]
  14.  
  15. '''Next we want to give each value in each list a unique identifier.'''
  16. # Loop through all keys
  17. for key in data.keys():
  18. values = data[key]
  19.  
  20. things = list(sorted(set(values), key=values.index))
  21.  
  22. for i, x in enumerate(data[key]):
  23. data[key][i] = things.index(x) + 1
  24. # print(data[key][i])
  25. NewData=data[key][i]
  26. print(NewData)
  27.  
  28. """Since csv.writerows() takes a list but treats it as a row, we
  29. need to restructure our
  30. data so that each row is one value from each list. This can be
  31. accomplished using zip():"""
  32.  
  33. with open("OuputFile.csv", "w") as outfile:
  34. writer = csv.writer(outfile)
  35. # Write headers
  36. writer.writerow(data.keys())
  37. # Make one row equal to one value from each list
  38. rows = zip(*data.values())
  39. # Write rows
  40. writer.writerows(rows)
  41.  
  42. job_Id FirstName LastName Address Email Date/Time
  43.  
  44. 1 snehil singh marathalli ss@gmail.com 12/10/2011:02:03:20
  45. 2 salman khan marathalli ss@gmail.com 12/11/2011:03:10:20
  46. 3 Amir khan HSR ar@gmail.com 11/22/2009:09:03:20
  47. 4 Rakhesh kumar HSR rakesh@gmail.com 09/15/2010:02:03:55
  48. 5 Ram sharma marathalli r@gmail.com 01/10/2014:12:03:20
  49. 6 Shyam shahi BTM ss@gmail.com 12/17/2012:01:03:20
  50. 7 salman khan HSR ss@gmail.com 11/08/2016:15:03:20
  51. 8 Amir khan BTM ar@gmail.com 07/10/2013:04:02:30
  52. 9 snehil singh Majestic sne@gmail.com 03/20/2018:02:03:20
  53.  
  54. job_Id FullName Address Email Date
  55. 1 1 1 1 12/10/2011
  56. 2 2 1 1 12/11/2011
  57. 3 3 2 2 11/22/2009
  58. 4 4 2 3 09/15/2010
  59. 5 5 1 4 01/10/2014
  60. 6 6 3 1 12/17/2012
  61. 7 2 2 1 11/08/2016
  62. 8 3 3 2 07/10/2013
  63. 9 1 4 5 03/20/2018
Add Comment
Please, Sign In to add comment