Guest User

Untitled

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