0

Ok so I asked for help with this code earlier today and got it fixed now I'm getting a weird error that I would like some advice or help getting fixed. The error that keeps popping up is

"Traceback (most recent call last):
  File "C:/Users/HatterX/Desktop/CSV_Reader_and_Writer2_vPC3", line 27, in <module>
    fr_name = frow['Franchise'].strip()
KeyError: 'Franchise'" 

any help on how to fix this would be greatly appreciated.

import csv, datetime

franchiseList = {}

with open('Franchise_Name_Scrub_List.csv', 'r') as ff:
fcf = csv.DictReader(ff)
for frow in fcf:
    franchiseList[frow['Misc Franchise Name']] = frow
for Frow in fcf:
    franchiselist[Frow['FRANCHISE Name - Directory']] = Frow

newrow={'Last Sale Date': '', 'Last Sale Amount': '', 'First Name': '', 'Last Name': '', 'Email Address': '', 'Franchise': '', 'State': '', 'Postal/Zip Code': '', 'Last Web Order ID': '', 'Date Added': '', 'Email Source':'', 'osg_web_dir': ''}
new_field_names = newrow.keys()

with open('SOR935csv_(1).csv', 'r') as f1, open('FACTS_bronto_import_add.csv', 'wb') as f2:
cf1 = csv.DictReader(f1, fieldnames=('CustNo1', 'CustNo2', 'LastOrderDate', 'LastOrderAmount', 'FirstName', 'LastName', 'UserNo', 'EmailAddress', 'Franchise', 'PrevOrderDate', 'PrevOrderAmount', 'State', 'ZIP', 'Amt1', 'Amt2', 'Amt3', 'SalesPerson', 'WEBID'))
cf2 = csv.DictWriter(f2, new_field_names)
cf2.writeheader()
for row in cf1:
    nr = newrow
    nr['Last Sale Date'] = row['LastOrderDate'].strip()
    nr['Last Sale Amount'] = row['LastOrderAmount'].strip()
    nr['First Name'] = row['FirstName'].strip()
    nr['Last Name'] = row['LastName'].strip()
    nr['Email Address'] = row['EmailAddress'].strip().split(',',1)[0]

    fr_name = frow['Franchise'].strip()
    if fr_name in franchiseList:
                nr['Franchise'] = franchiseList[fr_name]['FRANCHISE Name'].strip()
    else:
        nr['Franchise'] = 'SHOP'

    nr['State'] = row['State'].strip()
    nr['Postal/Zip Code'] = row['ZIP'].strip()
    nr['Last Web Order ID'] = row['WEBID'].strip()
    nr['Date Added'] = datetime.date.today().strftime('%m/%d/%Y')

    fr_Name = Frow['osg_web-dir'].strip()
    if fr_Name in franchiselist:
                nr['osg_web_dir'] = franchiselist[fr_Name]['FRANCHISE Name - Directory'].strip()
    else:
        nr['osg_web_dir'] = 'shop'

    #nr['osg_web_dir'] = row['SalesPerson'].strip()
    nr['Email Source'] = 'FACTSauto'
    print nr
    cf2.writerow(nr)


import csv

franchiseList = {}

with open('Franchise_Name_Scrub_List.csv', 'r') as ff:
fcf = csv.DictReader(ff)
for frow in fcf:
    franchiseList[frow['Misc Franchise Name']] = frow

newrow={'Last Sale Date': '', 'Last Sale Amount': '', 'First Name': '', 'Last Name': '', 'Email Address': '', 'Franchise': '', 'State': '', 'Postal/Zip Code': '', 'Last Web Order ID': '',  'osg_web_dir': ''}
new_field_names = newrow.keys()

with open('SOR935csv_(1).csv', 'r') as f3, open('FACTS_bronto_import_update.csv', 'wb') as f4:
cf3 = csv.DictReader(f3, fieldnames=('CustNo1', 'CustNo2', 'LastOrderDate', 'LastOrderAmount', 'FirstName', 'LastName', 'UserNo', 'EmailAddress', 'Franchise', 'PrevOrderDate', 'PrevOrderAmount', 'State', 'ZIP', 'Amt1', 'Amt2', 'Amt3', 'SalesPerson', 'WEBID'))
cf4 = csv.DictWriter(f4, new_field_names)
cf4.writeheader()
for row in cf3:
    nr = newrow
    nr['Last Sale Date'] = row['LastOrderDate'].strip()
    nr['Last Sale Amount'] = row['LastOrderAmount'].strip()
    nr['First Name'] = row['FirstName'].strip()
    nr['Last Name'] = row['LastName'].strip()
    nr['Email Address'] = row['EmailAddress'].strip().split(',',1)[0]

    fr_name = frow['Franchise'].strip()
    if fr_name in franchiseList:
                nr['Franchise'] = franchiseList[fr_name]['FRANCHISE Name'].strip()

    nr['State'] = row['State'].strip()
    nr['Postal/Zip Code'] = row['ZIP'].strip()
    nr['Last Web Order ID'] = row['WEBID'].strip()
    nr['osg_web_dir'] = row['SalesPerson'].strip()
    print nr
    cf4.writerow(nr)





#with open('SOR935csv_(1).csv', 'rb') as f1, \
#     open('FACTS_bronto_import_add.csv', 'ab') as f2:
#    for inrow in f1:
#        outrow = {
#        outrow[0] = str(inrow[2])
#        f2.write(', '.join(outrow))
1
  • Can you double check that your indentation is correct? I get an IndentationError just after the first with. And on the line fr_name = frow['Franchise'].strip(), you refer to frow but you're well outside of the for frow in fcf loop. (That's syntactically allowed, but rarely makes logical sense to do) Commented Mar 17, 2014 at 17:32

1 Answer 1

1

I think the line

fr_name = frow['Franchise'].strip()

Should be

fr_name = row['Franchise'].strip()
        # ^ note

frow is a for loop variable from elsewhere in your code (and will still have the last value it took in that loop), whereas row is the data you are currently processing.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.