提问人:HatterX 提问时间:3/17/2014 最后编辑:brianmearnsHatterX 更新时间:3/17/2014 访问量:186
混淆与缩进错误
Confusion with indentation error
问:
你好,我又在写这段代码的时候遇到了一个魔鬼。我修复了之前遇到的所有其他问题,但现在当我运行代码时,我收到缩进错误。附件是我的代码和不断弹出的错误图像,任何帮助找出此问题将不胜感激。
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)
校订
有几行存在缩进问题,多亏了这里的一些好心人,这是一个简单的修复,我只是过于复杂了。
答:
1赞
Martijn Pieters
3/17/2014
#1
使用以下命令运行脚本:
print -tt scriptname.py
并修复报告的所有错误。
然后将编辑器配置为仅使用空格进行缩进。
评论
if fr_name in franchiseList:
ValueError
self.writer.writerow()
with