Automatically delete the temp file on startup, change the pattern

The pattern or the pattern.sub is not correct and will not always match entire column
This works fine for most items, but school districts are noted for cropping this error up
This commit is contained in:
David Todd 2016-04-26 16:41:10 -05:00
parent 7d992b8866
commit 65cdaf28d2
No known key found for this signature in database
GPG Key ID: 48E847F18074C953
2 changed files with 6 additions and 4 deletions

View File

@ -48,6 +48,7 @@ def find_index(row=None, col=None):
return index return index
os.rename(config['import_path'], config['import_path']+'.orig') # Store the original import_path in a backup file os.rename(config['import_path'], config['import_path']+'.orig') # Store the original import_path in a backup file
os.remove(config['temp_path']) # Delete the temp file if it exists for some reason
with open(config['partner_path'], 'r') as partner_csv: with open(config['partner_path'], 'r') as partner_csv:
reader = csv.reader(partner_csv) reader = csv.reader(partner_csv)
@ -67,7 +68,7 @@ with open(config['partner_path'], 'r') as partner_csv:
partner_ids = {rows[name]:rows[id] for rows in reader} # Build a dict containing the company names as the keys and the __export__IDs as values partner_ids = {rows[name]:rows[id] for rows in reader} # Build a dict containing the company names as the keys and the __export__IDs as values
reader = None reader = None
# Open both the import_csv and temp_csv files # Open both the import_csv (read-only) and temp_csv (write-only) files
with open(config['import_path']+'.orig', 'r') as import_csv, open(config['temp_path'], 'w') as temp_csv: with open(config['import_path']+'.orig', 'r') as import_csv, open(config['temp_path'], 'w') as temp_csv:
reader = csv.reader(import_csv) reader = csv.reader(import_csv)
writer = csv.writer(temp_csv, lineterminator='\n') writer = csv.writer(temp_csv, lineterminator='\n')
@ -83,7 +84,8 @@ with open(config['partner_path'], 'r') as partner_csv:
writer.writerow(first_row) writer.writerow(first_row)
for lines in reader: for lines in reader:
# Taken from http://stackoverflow.com/a/2400577/5727514 and modified to support what I need # Taken from http://stackoverflow.com/a/2400577/5727514 and modified to support what I need
pattern = re.compile('|'.join(partner_ids.keys())) pattern = re.compile(r'\b(' + '|'.join(partner_ids.keys()) + r')\b')
#pattern = re.compile('|'.join(partner_ids.keys()))
lines[col] = pattern.sub(lambda x: partner_ids[x.group()], lines[col]) lines[col] = pattern.sub(lambda x: partner_ids[x.group()], lines[col])
writer.writerow(lines) writer.writerow(lines)