Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions convert_old_alw_to_new_structure.py
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:
Copy link
Contributor Author

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

#csv file to which new data is written
with open('csvfile.csv','w') as csvfile2:
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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']
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also please note that the format of the distination file should be
"External ID","Allowances","Allowances/Amount","Allowances/Contract" and not the above

#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']})

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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