Fix file open, write first_row, use UNIX newlines

Small fixes to get this script working
the checking logic in find_index is broken and has been disabled for now
This commit is contained in:
David Todd 2016-04-26 12:57:12 -05:00
parent 2e5e77ce53
commit 358a7d7da7
No known key found for this signature in database
GPG Key ID: 48E847F18074C953

15
main.py
View File

@ -36,9 +36,9 @@ config = {
# Find the index number of a column in a row and return that # Find the index number of a column in a row and return that
def find_index(row=None, col=None): def find_index(row=None, col=None):
if ((row == None) | (col == None)) | ((type(row) != type(str)) | (type(col) != type(str))): # if ((row == None) | (col == None)) | ((type(row) != type(str)) | (type(col) != type(str))):
raise ValueError('find_index requires both row and col to be set to strings') # raise ValueError('find_index requires both row and col to be set to strings')
exit(1) # exit(1)
index = None index = None
for i,j in enumerate(row): for i,j in enumerate(row):
@ -68,18 +68,19 @@ with open(config['partner_path'], 'r') as partner_csv:
reader = None reader = None
# Open both the import_csv and temp_csv files # Open both the import_csv and temp_csv files
with open(config['import_path'], '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) writer = csv.writer(temp_csv, lineterminator='\n')
first_row = next(reader) first_row = next(reader)
if config['column_to_change'] not in first_row: if config['column_to_change'] not in first_row:
raise IndexError('\'{}\' is not defined in your import_path csv file').format(config['column_to_change']) raise IndexError(config['column_to_change']+' is not defined in your import_path csv file')
exit(1) exit(1)
col = find_index(first_row, config['column_to_change']) col = find_index(first_row, config['column_to_change'])
if col != None: if col != None:
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('|'.join(partner_ids.keys()))