混淆密钥错误python

时间:2014-03-17 17:26:29

标签: python keyerror

好的,所以我今天早些时候请求帮助解决这个问题并立即修复我得到一个奇怪的错误,我想要一些建议或帮助修复。不断弹出的错误是

"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'" 

如何解决这个问题的任何帮助将不胜感激。

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 个答案:

答案 0 :(得分:1)

我认为该行

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

应该是

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

frow是来自代码中其他位置的for循环变量(并且仍将具有该循环中的最后一个值),而row是您当前正在处理的数据