2015-10-28 34 views
0

我的代碼返回:Python的CSV:類型錯誤不支持的操作「斜面和列表」

Traceback (most recent call last): 
    File "/Users/Justin/Desktop/Program 5/Weather_Program#5.py", line 111, in <module> 
    month = sum(january)/10 
TypeError: unsupported operand type(s) for +: 'int' and 'str' 

我確切地知道,爲什麼這個錯誤發生,但我不知道如何解決它。任何想法將不勝感激。

我在csv中讀取的數據是左欄中的日期和右欄中的降水量。 20000101將在2001年1月1日,0將是0英寸的雨。 Na被排除,它在第二個while循環中正常工作。有一次,我轉換的數字,以花車我碰上錯誤

20000101 0 

20000102 NA 

20000103 12.2 

20000104 0.3 

代碼:

import csv 

january = [] 
february = [] 
march  = [] 
april  = [] 
may  = [] 
june  = [] 
july  = [] 
august = [] 
september = [] 
october = [] 
november = [] 
december = [] 

def read_cell(column, row): 
    """Input x & y coordinate for CSV file: Returns information in cell""" 
    with open('precipitation.csv', 'r') as f: 
     reader = csv.reader(f) 
     y_count = 0 
     for n in reader: 
      if y_count == row: 
       cell = n[column] 
       return cell 
      y_count += 1 


stormy = 0 
row_number = 0 

while stormy < 1: 
    with open('precipitation.csv', 'r') as f: 
      date = read_cell(0,row_number) 
      rain = read_cell(1,row_number) 
      if date == None: 
       stormy += 1 
       break 

      elif date[4:6] == "01": 
       january.append(read_cell(1,row_number)) 
       row_number += 1     

      elif date[4:6] == "02": 
       february.append(read_cell(1,row_number)) 
       row_number += 1 

      elif date[4:6] == "03": 
       march.append(read_cell(1,row_number)) 
       row_number += 1 

      elif date[4:6] == "04": 
       april.append(read_cell(1,row_number)) 
       row_number += 1 

      elif date[4:6] == "05": 
       may.append(read_cell(1,row_number)) 
       row_number += 1    

      elif date[4:6] == "06": 
       june.append(read_cell(1,row_number)) 
       row_number += 1    

      elif date[4:6] == "07": 
       july.append(read_cell(1,row_number)) 
       row_number += 1 

      elif date[4:6] == "08": 
       august.append(read_cell(1,row_number)) 
       row_number += 1    

      elif date[4:6] == '09': 
       september.append(read_cell(1,row_number)) 
       row_number += 1    

      elif date[4:6] == '10': 
       october.append(read_cell(1,row_number)) 
       row_number += 1    

      elif date[4:6] == '11': 
       november.append(read_cell(1,row_number)) 
       row_number += 1 

      elif date[4:6] == '12': 
       december.append(read_cell(1,row_number)) 
       row_number += 1 

while "NA" in january: 
    january.remove("NA") 

    while "NA" in february: 
     february.remove("NA")  

     while "NA" in march: 
      march.remove("NA")  

      while "NA" in april: 
       april.remove("NA") 

       while "NA" in may: 
        may.remove("NA") 

        while "NA" in june: 
         june.remove("NA") 

         while "NA" in july: 
          july.remove("NA") 

          while "NA" in august: 
           august.remove("NA") 

           while "NA" in september: 
            september.remove("NA") 

            while "NA" in october: 
             october.remove("NA") 

             while "NA" in november: 
              november.remove("NA") 

              while "NA" in december: 
               december.remove("NA") 


january = [float(i) for i in january] 
january = sum(float(january))/10 
print(january) 

回答

0

所以我覺得這是你的情況有

>>> sum([0,'NA', 12.2, 0.3]) 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
TypeError: unsupported operand type(s) for +: 'int' and 'str' 

您可以先篩選值通過這樣的總結。它被稱爲「發生器表達式」

>>> sum(x for x in [0,'NA', 12.2, 0.3] if x != 'NA') 
12.5 

即,

month = sum(x for x in january if x != 'NA')/10 
+0

這只是列表的一個例子。我正在處理5000+個單元,所以這不起作用。 – Wakedude

+0

@Wakedude,它不一定是一個文字列表 - 任何返回序列的表達式都可以。 –

+0

謝謝!這爲我修好了。真的很感謝幫助。 – Wakedude

相關問題