-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ADD] initial commit of script #1
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import csv | ||
#csv file from which the original data is read | ||
with open('hr.contract (8).csv') as csvfile1: | ||
#csv file to which new data is written | ||
with open('csvfile.csv','w') as csvfile2: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again please do not hardcode here... if it can be supplied via command line, great if not please introduce variables at the top of the file; also same point on descriptive variable as it makes your code easier to read. Also line 3 and line 5 can be written as one: with open('a',) as a, open('b', 'w') as b:
do_something() |
||
#the keys in the new csv file | ||
fieldnames = ['External ID', 'Allowances', 'Allowances/Amount'] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also please note that the format of the distination file should be |
||
#the row values in the old csv file are assigned to "reader" | ||
reader = csv.DictReader(csvfile1) | ||
#write the headers in the new csv file | ||
writer = csv.DictWriter(csvfile2,fieldnames=fieldnames) | ||
writer.writeheader() | ||
#main data conversion | ||
for row in reader: | ||
writer.writerow({'External ID': row['External ID'], | ||
'Allowances': '__export__.hr_payroll_allowance_line_1', | ||
'Allowances/Amount': row['Medical']}) | ||
writer.writerow({'External ID': '', | ||
'Allowances': '__export__.hr_payroll_allowance_line_2', | ||
'Allowances/Amount': row['Transport']}) | ||
writer.writerow({'External ID': '', | ||
'Allowances': '__export__.hr_payroll_allowance_line_3', | ||
'Allowances/Amount': row['Rent']}) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please do not hard code fieldnames, this can easily be read for the the csv.DictReader instance e.g. reader = csv.DictReader(x)
fieldnames = reader.fieldnames As explained yesterday, create a dict that stores the mapping of Fieldnames in sourcefiel to external IDs in destination file and use that to build the destination file. Also if I run this it won't give the output file in the format I sent you... it is important that the format is exactly the same |
||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do not hard code the file, create a variable at the top of the file that stores the filename... also please provide descriptive variable names... e.g. instead of csvfile1 it can be called csvsourcefile