2015-11-20 40 views
0

請耐心等待,我的問題並不像看起來那麼令人望而生畏。一切都很完美,直到第四節結束。如何記錄「n」個數以及相應的時間?初學者任務

def readTemperature(): 
"""It will read the string, extract the temperature, then return the data 
as a float. 
""" 
data = readTemperatureSensor.randomTemperatureGenerator() 
data = str(data) 
for line in data: 
    if "t" in line: 
     tempBit = data.split("t") 
     tempString = tempBit[1].replace("=", "") 
return float(tempString)/1000 

上面的代碼只是讀取溫度。

def getTemperatures(n): 
"""Returns a list of floats that contain a certain number (n) of 
temperature readings that have been sampled at an interval of one second. 
""" 
temperaturelist = [] 
for data in range(n): 
    temperaturelist.append(float(readTemperature())) 
    time.sleep(1) 
return temperaturelist 

上面的代碼得到了一堆相隔一秒的溫度。

def getTimeStamp(): 
    """Returns the current system date and time as a string with the format 
    YYYY_MM_DD_HH_MM_SS. 
    """ 
    return time.strftime("%Y_%m_%d_%H_%M_%S") 

只是一個簡單的時間戳。

現在,我不知道該怎麼做。

def logTemperatures(n): 
"""This function will record "n" temperature readings and their 
corresponding time-stamps, sampled at 1 seconds intervals. 
""" 

基本上,它要我創建一個文本文件(這是我能做到),也希望我讀作「n」的溫度和具有它的相應的時間也是如此。如果它只是記錄溫度的設定量,那麼我可以做到這一點,我的代碼是這樣的:

f = open('logTemperatures.txt', 'w') 
type(f) 
myList = getTemperatures(3) 
currentTime = time.time() 
f.write(str(time.strftime("%Y_%m_%d_%H_%M_%S", 
          time.localtime(currentTime - 0.03 * 60)))) 
f.write(',') 
f.write(" ") 
f.write(str(myList[0])) 
f.write('\n') 
f.write(str(time.strftime("%Y_%m_%d_%H_%M_%S", 
          time.localtime(currentTime - 0.02 * 60)))) 
f.write(',') 
f.write(" ") 
f.write(str(myList[1])) 
f.write('\n') 
f.write(str(time.strftime("%Y_%m_%d_%H_%M_%S", 
          time.localtime(currentTime - 0.01 * 60)))) 
f.write(',') 
f.write(" ") 
f.write(str(myList[2])) 
f.close() 

logTemperatures(3) 

有很多f.write功能,因爲我還是個初學者,但我現在知道我可以使用+按鈕將它鏈接起來。

該文本文件是這樣的:


時間,溫度1

時間+ 1秒,溫度2

時間+ 2秒,溫度3


它可能會達到10個溫度或只有1個。很明顯,上面的代碼不會工作因爲它只會在3個溫度而不是「n」個溫度下進行。我怎樣才能修改它,以便在「n」個溫度下完成?

+0

使用循環,就像你在'getTemperatures(n)'中做的那樣。 –

回答

-2

一個For環就是你需要在不同的線分開這些寫入。您的getTemperatures函數中已經有一個迭代的for循環,它會創建一個長度爲n的數組。現在你需要一個for循環,通過迭代指數,像這樣:

for i in range(len(myList)): 

    f.write(str(time.strftime("%Y_%m_%d_%H_%M_%S", 
         time.localtime(currentTime + i)))) 
    f.write(',') 
    f.write(" ") 
    f.write(str(myList[i])) 
    f.write('\n') 

注意i是一個數字(即0,1,2 ...),所以我們加入icurrentTime獲取時間,我們得到來自陣列的溫度如此myList[i]。我相信currentTime是在幾秒鐘內,所以加入i會增加一秒。

因此,在您的logTemperatures(n)函數中,您可以調用getTemperatures(n)來獲取溫度數組。然後使用類似於上面顯示的代碼來遍歷數組,以將結果寫入文件。

希望有所幫助,原諒我粗糙的答案。第一次發佈答案:)

+0

工作就像一個魅力!有時間分析它在地球上的工作原理。非常感謝你! :D – Scarthia

+0

很高興能幫到你!請標記爲正確:) –

-3

在Python 3

n = input("Enter the number: ") 

然後:

myList = getTemperatures(n) 
currentTime = time.time() 

for data in myList: 
    f.write(str(time.strftime("%Y_%m_%d_%H_%M_%S", 
         time.localtime(currentTime - 0.02 * 60)))) 
    f.write(',') 
    f.write(" ") 
    f.write(str(data)) 
    f.write('\n') 

f.close() 
logTemperatures(n) 
0

您可以編寫一個函數,它在溫度和迭代列表中的每個項目的列表,爲它創造一個條目日誌文件。

def logTemps(tempList): 
    with open('logTemperatures.txt', 'w') as outfile: 
     currentTime = time.time() 
     for i, temp in enumerate(tempList): 
      outfile.write(str(time.strftime("%Y_%m_%d_%H_%M_%S", 
           time.localtime(currentTime - 60 * (0.01 * (len(tempList) - i)))))) 
      outfile.write(', ' + str(temp) + '\n') 

你會從前面的例子中輸入您的myList給函數調用的方法:

logTemps(myList) 

兩件事情一提的是我所做的:

使用with open('example.txt', 'w') as yourVariableNameHere塊,而不是open()close()。使用文件的with塊會自動關閉該塊末尾的文件,並清楚地向任何閱讀代碼的人指出您正在做什麼。

我做了0.01*the length of the list - the current index,但我希望你意識到這意味着你將最終得到currentTime - 3, currentTime - 2, and currentTime - 1 ...而不是currentTime, currentTime + 1, currentTime + 2你似乎認爲。

也有真的沒有理由,所以我只是他們組合成一個outfile.write('...')

+1

非常感謝您的幫助!我花了一點時間來圍繞你寫的東西,抱歉。用你的「for i,temp in enumerate(tempList)」line雖然,我收到一個「int不可迭代」的錯誤。我只使用過range(),所以我不明白你的腳本有什麼問題。 – Scarthia

+0

@Scarthia是你提供溫度清單的tempList嗎?或者它只是一個整數?它需要是一個列表,例如格式爲[1,2,3,4]。另外,你確定你確實寫過'enumerate'嗎?因爲如果沒有,它會給你那個錯誤。 – Matthew

+0

所有這些都包含小數位,所以它們都不是整數。我將tempList更改爲getTemperatures(第二部分),因爲這正是我正在處理的內容,並且它將它作爲方括號中的列表返回,如您所示。再次感謝您的幫助! :) – Scarthia