Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import csv
- from collections import OrderedDict
- def main():
- #npa
- npa = {}
- with open('npa.csv') as f:
- for line in f:
- current_line = line.split(',')
- current_line[1] = current_line[1].strip()
- if current_line[0] == "NPA":
- print "Skipping title"
- elif current_line[1] in npa: # if key is in dictionary
- print("Two or locations mapped to a NPA")
- current_line[0] = current_line[0] + ", " + npa.get(current_line[1])
- npa.update({current_line[0] : current_line[1]}) # add new NPA with location
- else:
- npa.update({current_line[0] : current_line[1]}) # add new Location and NPA to npa dictionary
- #sample_date
- sample = {}
- with open('sample_data.csv') as f:
- for line in f:
- current_line = line.split(',')
- current_line[2] = int(current_line[2].strip())
- if current_line[0][1:4] in sample: # if key is in dictionary
- current_line[2] = current_line[2] + int(sample.get(current_line[0][1:4])) # add the duration onto the previous entry
- sample.update({current_line[0][1:4] : current_line[2]}) # add new NPA with duration
- else:
- sample.update({current_line[0][1:4] : current_line[2]}) # add new NPA and duration to dictionary
- #joined_data
- joined = {}
- sortnpa = OrderedDict(sorted(npa.items(), key=lambda t: t[0]))
- sortsample = OrderedDict(sorted(sample.items(), key=lambda t: t[0]))
- for key, value in sortnpa.items():
- if value in joined:
- if type(sortsample.get(key)) is int and type(joined.get(value)) is int:
- new = sortsample.get(key) + int(joined.get(value)) # add extra minutes if in dictionary
- joined.update({value : new}) # add new entry
- else:
- joined.update({value : sortsample.get(key)}) # add new entry
- #csv
- with open('joined_data.csv', 'wb') as csv_file:
- writer = csv.writer(csv_file)
- writer.writerow(["Location", "Minutes"])
- for key, value in joined.items():
- writer.writerow([key, value]) # write location and duration in minutes
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement