Question

Hello again I am having a devil of a time with this code that I'm writing. I got everything else fixed that I was having issues with before but now when I run the code i get a indentation error. Attached is my code and a image of the error that keeps popping up any help to figure out this problem 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

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 = row['Franchise'].strip()
    if fr_name in franchiseList:
    nr['Franchise'] = franchiseList[fr_name]['FRANCHISE Name'].strip()
nr['Franchise'] = row['Franchise'].strip()
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')
nr['osg_web_dir'] = row['SalesPerson'].strip()
nr['Email Source'] = 'FACTSauto'
print nr
cf2.writerow(nr)

import csv

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]
    nr['Franchise'] = row['Franchise'].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)

enter image description here

Revision

There was a indentation issue on several of the lines and thanks to some kind people on here it was a easy fix that i was jsut over complicating.

Was it helpful?

Solution

Run your script with:

print -tt scriptname.py

and fix all errors that reports.

Then configure your editor to use only spaces for indentation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top