2017-02-14 77 views
-1

我正在處理需要csv文件的代碼,然後爲csv文件中的每一行返回一個數據點列表。列表將按日期和地點排序。Python列表將打印但不返回

該代碼使列表如何我想要它,但調用時不返回列表。

第一個函數讀取文件並生成數據點。第二個函數調用第一,排序和(希望)返回數據

def CreateDataStructure(data): 
allData=[] 
with open(data,'r') as data: 

    dataRead=data.readlines() 
    for line in dataRead[1:]: 
     splitList=line.split(",") 
     dataPoint =[splitList[25],splitList[1],{splitList[19]:splitList[9]}] 
     allData.append(dataPoint) 
sortedData=sorted(allData) 
return sortedData 


def compileData(filename,counter,sortedData):  
    if counter==0: 
     sortedData=CreateDataStructure(filename) 
     compileData(filename,1,sortedData) 
    else: 
     if counter<len(sortedData): 

      if sortedData[0][0]==sortedData[1][0] and sortedData[0][1]==sortedData[1][1]:#change these back 
       compDict=dict(list(sortedData[0][2].items())+list(sortedData[1][2].items())) 
       sortedData[0]=[sortedData[0][0],sortedData[0][1],compDict]  
       sortedData.pop(1) 
       compileData(filename,counter,sortedData) 
       counter=counter+1 
      else: 
       sortedData+=[sortedData.pop(0)] 
       counter=counter+1 
       compileData(filename,counter,sortedData) 
     else: 
      from itertools import groupby 
      for key, locationGroup in groupby(sortedData, lambda x: x[0]): 
       bigList=[] 
       smallList=[] 
        for date in locationGroup: 
        smallList.append(date) 
        bigList.append(smallList) 
      print bigList 
      return bigList 

print compileData("fakeData.csv",0,[]) 

當我運行這段代碼,它打印我想要的東西(biglist,這是我在你想知道的情況下粘貼以下),但返回None( noneType對象)。 爲什麼返回和打印給出兩個不同的東西,我該如何解決這個問題

[[['744701', '40974', {'Alkalinity': '234'}], ['744701', '41018', {'Alkalinity': '252'}], ['744701', '41058', {'Alkalinity': '270.53'}]], [['744701', '40974', {'Alkalinity': '234'}], ['744701', '41018', {'Alkalinity': '252'}], ['744701', '41058', {'Alkalinity': '270.53'}]], [['744701', '40974', {'Alkalinity': '234'}], ['744701', '41018', {'Alkalinity': '252'}], ['744701', '41058', {'Alkalinity': '270.53'}]]]

+2

在代碼中只使用了一個return語句('compileData()')。在某些情況下,您的if語句沒有返回值。 –

+0

我猜你還想交換'bigList = []'和'key,locationGroup ...'這兩行。 另外,您可能需要的是每次調用'compileData'前的返回。 最後但並非最不重要:請在發佈之前檢查代碼格式。 – nitzel

+0

我會提供使用'pandas'來做這種事情的建議,'pandas.read_csv'會在csv文件中讀取,你可以很容易地做'groupby' - 我不完全確定從csv到所需的輸出列表雖然 –

回答

0

compileData函數執行的多個分支,這意味着它可以在多個點返回。然而,只有其中之一counter !=0 and counter >= len(sortedData)即最後else分支曾經返回任何

例如:

if counter==0: 
    sortedData=CreateDataStructure(filename) 
    compileData(filename,1,sortedData) 
即使是 compileData呼叫管理,以達到最後 else分支,即一個

打印並返回結果,實際上沒有結果,這意味着在compileData(filename,1,sortedData)完成後,它所返回的數據將被丟棄,並繼續執行功能,直到它最終到達末尾並隱含返回None

這應該可以解決問題:

def compileData(filename,counter,sortedData):  
    if counter==0: 
     sortedData=CreateDataStructure(filename) 
     return compileData(filename,1,sortedData) 
    else: 
     if counter<len(sortedData): 

      if sortedData[0][0]==sortedData[1][0] and sortedData[0][1]==sortedData[1][1]:#change these back 
       compDict=dict(list(sortedData[0][2].items())+list(sortedData[1][2].items())) 
       sortedData[0]=[sortedData[0][0],sortedData[0][1],compDict]  
       sortedData.pop(1) 
       counter=counter+1 
       return compileData(filename,counter,sortedData) 
      else: 
       sortedData+=[sortedData.pop(0)] 
       counter=counter+1 
       return compileData(filename,counter,sortedData) 
     else: 
      from itertools import groupby 
      for key, locationGroup in groupby(sortedData, lambda x: x[0]): 
       bigList=[] 
       smallList=[] 
        for date in locationGroup: 
        smallList.append(date) 
        bigList.append(smallList) 
      print bigList 
      return bigList 

更新

你可以讓你的函數(主觀)有點容易被returning early代替嵌套幾個ifs理解。你的函數看起來像這樣:

from itertools import groupby 
... 
def compileData(filename,counter,sortedData): 
    if counter==0: 
     sortedData=CreateDataStructure(filename) 
     return compileData(filename,1,sortedData) 

    if counter<len(sortedData): 
     if sortedData[0][0]==sortedData[1][0] and sortedData[0][1]==sortedData[1][1]:#change these back 
      compDict=dict(list(sortedData[0][2].items())+list(sortedData[1][2].items())) 
      sortedData[0]=[sortedData[0][0],sortedData[0][1],compDict]  
      sortedData.pop(1) 
      counter=counter+1 
      return compileData(filename,counter,sortedData) 

     sortedData+=[sortedData.pop(0)] 
     counter=counter+1 
     return compileData(filename,counter,sortedData) 

    for key, locationGroup in groupby(sortedData, lambda x: x[0]): 
     bigList=[] 
     smallList=[] 
      for date in locationGroup: 
      smallList.append(date) 
      bigList.append(smallList) 

    print bigList 
    return bigList 
+0

嗨,這工作!非常感謝! –

+0

你是否看到其他問題? –