0

嘿,我是 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()
4

3 回答 3

0

这正是这里发生的事情:

if office_list.state == False

您正在调用定义为.state的实例list

office_list= ['Office Phone']

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

似乎您要么犯了一个基本错误并且没有意识到/发现它,要么您期望某些“魔术”会自动发生,而无需.state首先了解语法的含义。唯一调用state的是你的变量state,它确实包含布尔值,但它现在可以作为列表方法访问——我不确定是什么让你认为它会是。

如果您需要保留 3 个不同的状态,每个列表一个,只需使用 3 个不同的状态变量,称为state_officealt_state然后fax_state访问它们。永远不要在不了解发生了什么的情况下尝试做神奇的事情,并期望 Python 能按照你的意思去做。

于 2013-10-22T20:41:50.160 回答
0

当你这样做

if office_list.state == False:

它寻找的state是 的一个属性office_list,但事实并非如此。我在上面看到你在几个地方设置了state等于。false我建议制作 3 个不同的变量并为您使用 if 语句。A office_list_statealt_phone_list_state和 afax_list_state并将它们用作您的布尔值。当你到达你的 if 块时,你需要确保它们有一个值,所以你可能想给它们一个初始值True. 我没有仔细检查代码以了解您在那里需要什么。

于 2013-10-22T20:41:43.973 回答
0

欢迎来到 StackOverflow!

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

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

然后,而不是做state = False,做states['office_phone'] = False,例如。当您想检查状态时,请执行if states['office_phone']:.

希望有帮助。快乐编码!

于 2013-10-22T20:41:45.350 回答