こんにちは、Python でのコーディングは初めてです。csv を解析して、適切な電話番号と不適切な電話番号を分離しようとしています。これが私がこれまでに持っているものです。次のエラーが表示されます: * AttributeError: 'list' object has no attribute 'state' * これを修正する方法を教えてください。クラスが定義されていないことはわかっていますが、他にデータを分離する方法がわかりません。
# Description: cleans phone number field and returns fields that need manual edits
# Date Modified: 10/22/13
#Working on making two separate files...good and bad phone.
import csv
import string
import time
import re
import pprint
import codecs
start_time = time.time()
# remove all non-numeric characters from phone numbers
all=string.maketrans('','')
nodigs=all.translate(all, string.digits + string.ascii_letters + " ")
nospace=all.translate(all, string.digits + string.ascii_letters)
def main():
# creates new file to dump clean data
fout = codecs.open('clean_phone.csv', 'w', 'latin-1')
writer = csv.writer(fout)
#Create dictionary for easy reading/location
office_list= ['Office Phone']
alt_list= ['Alternative Phone']
fax_list= ['Fax']
# Creates boolean to allow for delegation into good/bad csv files
state = True
# Begin parsing Office Phone
with codecs.open('companies.csv', 'r', 'latin-1') as f:
reader = csv.reader(f)
headers = reader.next()
id_index=headers.index("ID")
office_phone_index=headers.index("Office Phone")
alt_phone_index=office_phone_index + 1
fax_index=office_phone_index + 2
condensed_header = [headers[id_index], headers[office_phone_index], headers[alt_phone_index], headers[fax_index]]
writer.writerow(condensed_header)
i=2
for row in reader:
# Clean Office Phone
phoneNumber= row[office_phone_index]
goodNumber=phoneNumber.translate(all, nodigs)
if len(goodNumber)>12:
numberParts= goodNumber.rpartition("%x")
if numberParts[0]:
goodNumber=numberParts[0]
else:
state = False
office_list.append("row %d: %s" %(i, goodNumber))
row[office_phone_index]=goodNumber.translate(all, nospace)
# Clean Alternate Phone
phoneNumber2= row[alt_phone_index]
goodNumber2=phoneNumber2.translate(all, nodigs)
if len(goodNumber2)>12:
numberParts2= goodNumber2.rpartition("%x")
if numberParts2[0]:
goodNumber2=numberParts2[0]
else:
state = False
alt_list.append("row %d: %s" %(i, goodNumber2))
row[alt_phone_index]=goodNumber2.translate(all, nospace)
# Clean FAX
phoneNumber3= row[fax_index]
goodNumber3=phoneNumber3.translate(all, nodigs)
if len(goodNumber3)>12:
numberParts3= goodNumber3.rpartition("%x")
if numberParts3[0]:
goodNumber3=numberParts[0]
else:
state = False
fax_list.append("row %d: %s" %(i, goodNumber3))
row[fax_index]=goodNumber3.translate(all, nospace)
# Write Row (write to a good or bad list)
condensed_row = [row[id_index],row[office_phone_index], row[alt_phone_index], row[fax_index]]
#Bad Phone List
if office_list.state == False:
fout = codecs.open('manual_office_phone_fix.csv', 'w', 'latin-1')
writer.writerow(condensed_row)
if alt_phone_list.state == False:
fout = codecs.open('manual_alt_phone_fix.csv', 'w', 'latin-1')
writer.writerow(condensed_row)
if fax_list.state == False:
fout = codecs.open('manual_fax_phone_fix.csv', 'w', 'latin-1')
writer.writerow(condensed_row)
#Good Phone List for Manual Edit
else:
writer.writerow(condensed_row)
#Move to next item
i+=1
#Print Results to the Console
print "The following phone numbers need manual review." + "\n"
pprint.pprint(office_list)
pprint.pprint(alt_list)
pprint.pprint(fax_list)
fout.close()
main()