2013-10-22 26 views
0

嘿,我是新來的編碼在Python中。嘗試通過csv進行解析並將不錯的電話號碼與不好的電話號碼分開。這是我到目前爲止。我得到一個錯誤:* AttributeError:'list'object has no attribute'state' * 任何想法如何解決這個問題..我知道我沒有定義類,但我不知道如何我可以分開數據。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() 
+0

發佈錯誤的完整回溯。 – kindall

+0

也發佈較少的代碼。請參閱http://sscce.org/以瞭解我的意思。 – SethMMorton

回答

0

當你

if office_list.state == False: 

其尋找state是的office_list一個屬性,它不是。我在上面看到你在幾個地方設置了state等於false。我會建議製作3個不同的變量,並使用這些變量,如果語句。 A office_list_statealt_phone_list_statefax_list_state,並將它們用作布爾值。當你到達if塊時,你需要確保它們有價值,所以你可能想給它們一個初始值True。我沒有仔細檢查代碼以便知道您需要什麼。

0

歡迎來到StackOverflow!

看起來你需要跟蹤幾個不同領域的狀態。你試過字典嗎?像這樣的事情在你的循環的頂部:

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

然後,而不是做state = False,做states['office_phone'] = False,例如。當你想檢查狀態時,請執行if states['office_phone']:

希望有所幫助。快樂的編碼!

0

這正是這裏發生了什麼:

if office_list.state == False 

你定義爲

office_list= ['Office Phone'] 

不具有state方法list實例調用.state。此外,office_list似乎包含字符串,它也沒有.state方法。

看起來好像你已經犯了一個基本的錯誤,沒有意識到/發現它,或者你希望在不理解語法.state意味着什麼的情況下自動發生某些「魔術」。唯一叫做state的是你的變量state,它確實保存布爾值,但它現在可以作爲列表方法訪問 - 我不確定是什麼讓你認爲它會是。

如果您需要保留3種不同的狀態,每個列表一個,請使用3個不同的狀態變量,分別稱爲state_office,alt_statefax_state,然後訪問它們。不要試圖在不瞭解正在發生的事情的情況下做出神奇的事情,並期望Python能夠做到你的意思。