Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import csv
- # open file
- with open('ProgramData.csv', 'r') as csvfile:
- # add two more columns
- new_csv = csvfile.read().replace('Location', 'City,State')
- # replace the " " for two fields
- new_csv = new_csv.replace('\"', ' ')
- with open('Normalized.csv', 'w') as csv_write:
- csv_write.write(new_csv)
- with open('Normalized.csv', 'r') as csvnorm:
- # Get the reader
- reader = csv.DictReader(csvnorm)
- # Header to write
- header = reader.fieldnames
- with open('Normalized.csv', 'w') as csv_write:
- writer = csv.DictWriter(csv_write, fieldnames=header)
- writer.writeheader()
- # For each row
- for row in reader:
- print(row['Cost'], row['Months full-time'])
- try:
- row['Cost/Month'] = int(row['Cost']) // int(row['Months full-time'])
- except ValueError:
- row['Cost/Month'] = 0
- except TypeError:
- row['Cost/Month'] = 0
- #print('Cost per month:',row['Cost/Month'], '\n')
- # Get the writer
- writer = csv.DictWriter(csv_write, fieldnames=row.keys())
- writer.writerows([row])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement