Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- with open("input2.csv", 'r',encoding="utf8") as csvfile:
- # creating a csv reader object
- reader = csv.DictReader(csvfile, delimiter=',')
- '''We then restructure the data to be a set of keys with list of values {key_1: [], key_2: []}:'''
- data = {}
- for row in reader:
- # print(row)
- for header, value in row.items():
- try:
- data[header].append(value)
- except KeyError:
- data[header] = [value]
- '''Next we want to give each value in each list a unique identifier.'''
- # Loop through all keys
- for key in data.keys():
- values = data[key]
- things = list(sorted(set(values), key=values.index))
- for i, x in enumerate(data[key]):
- data[key][i] = things.index(x) + 1
- # print(data[key][i])
- NewData=data[key][i]
- print(NewData)
- """Since csv.writerows() takes a list but treats it as a row, we
- need to restructure our
- data so that each row is one value from each list. This can be
- accomplished using zip():"""
- with open("OuputFile.csv", "w") as outfile:
- writer = csv.writer(outfile)
- # Write headers
- writer.writerow(data.keys())
- # Make one row equal to one value from each list
- rows = zip(*data.values())
- # Write rows
- writer.writerows(rows)
- job_Id FirstName LastName Address Email Date/Time
- 1 snehil singh marathalli ss@gmail.com 12/10/2011:02:03:20
- 2 salman khan marathalli ss@gmail.com 12/11/2011:03:10:20
- 3 Amir khan HSR ar@gmail.com 11/22/2009:09:03:20
- 4 Rakhesh kumar HSR rakesh@gmail.com 09/15/2010:02:03:55
- 5 Ram sharma marathalli r@gmail.com 01/10/2014:12:03:20
- 6 Shyam shahi BTM ss@gmail.com 12/17/2012:01:03:20
- 7 salman khan HSR ss@gmail.com 11/08/2016:15:03:20
- 8 Amir khan BTM ar@gmail.com 07/10/2013:04:02:30
- 9 snehil singh Majestic sne@gmail.com 03/20/2018:02:03:20
- job_Id FullName Address Email Date
- 1 1 1 1 12/10/2011
- 2 2 1 1 12/11/2011
- 3 3 2 2 11/22/2009
- 4 4 2 3 09/15/2010
- 5 5 1 4 01/10/2014
- 6 6 3 1 12/17/2012
- 7 2 2 1 11/08/2016
- 8 3 3 2 07/10/2013
- 9 1 4 5 03/20/2018
Add Comment
Please, Sign In to add comment