AttributeError:'list'对象没有属性'state'

时间:2013-10-22 20:36:33

标签: python csv

嘿,我是蟒蛇编码的新手。试图通过csv解析并从糟糕的电话号码中分离出好的电话号码。这是我到目前为止所拥有的。我收到错误: * AttributeError:'list'对象没有属性'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()

3 个答案:

答案 0 :(得分:0)

当你这样做时

if office_list.state == False:

它正在寻找state属于office_list的属性,而不是state。我在上面看到您在一些地方将false设置为等于office_list_state的位置。我建议制作3个不同的变量并使用它们if if statement。 alt_phone_list_statefax_list_stateTrue并将其用作您的布尔值。当您到达if块时,您需要确保它们具有值,因此您可能希望为它们提供初始值{{1}}。我没有仔细检查代码,知道你需要什么。

答案 1 :(得分:0)

欢迎使用StackOverflow!

看起来您需要跟踪几个不同字段的状态。你试过一本字典吗?在你的循环顶部有这样的东西:

states = {'office_phone': True,
          'alt_phone': True,
          'fax': True} 

然后,不要执行state = False,而是执行states['office_phone'] = False。如果要检查状态,请执行if states['office_phone']:

希望有所帮助。快乐的编码!

答案 2 :(得分:0)

这正是这里发生的事情:

if office_list.state == False

您在定义为

.state个实例上调用list
office_list= ['Office Phone']

没有state方法。此外,office_list似乎包含字符串,它们也没有.state方法。

好像你犯了一个基本的错误并且没有意识到/发现它,或者你期望某些“魔法”自动发生而不理解第一个语法.state的含义地点。唯一被称为state的是你的变量state,它确实保存了布尔值,但它现在可以作为列表方法访问 - 我不确定是什么让你认为它会是。

如果您需要保留3个不同的状态,每个列表一个,只需使用3个不同的状态变量state_officealt_statefax_state,然后访问它们。并且永远不要试图在不了解正在发生的事情的情况下做出神奇的事情,并希望Python能够做到你的意思。