2016-07-05 76 views
0

我正在破解我的第一個Python項目。我正在運行下面的Python代碼,並且遇到了linecache.getline問題。我希望它打開stoarage.txt文件。檢查數字是否爲0.如果是,請將其更改爲1 - 播放音樂並打開LED。然後將其切換到2,每當我關機並啓動我的Raspberry Pi時,它都不會再播放音樂。任何建議都會超級棒。從外部.txt文件讀取一行的linecache的Python問題

我storage.txt是結束了這樣的:

0 

0 

0 

我認爲它應該是這樣的:

2 
0 
0 

我的Python代碼如下所示:

import requests 
import pygame 
import RPi.GPIO as GPIO 
from BeautifulSoup import BeautifulSoup 
import linecache 


pygame.mixer.init() 
pygame.mixer.music.load('chaching.wav') 


GPIO.setmode(GPIO.BCM) 
#Im using pin 23 for the led. 
GPIO.setup(21, GPIO.OUT) 
#Im using pin 4 as a basic switch in replacement for a motion sensor. 
GPIO.setup(4, GPIO.IN) 

#Cleans up the number taken from my php file. 
Soup = BeautifulSoup 

#Subscriber number 
sub_num = 0 

#goal one value 
goal1 = 93 
#goal1 celebration flag pulled from the txt file. Line 1 of the doc. 
goal1cel = linecache.getline('storage.txt', 1) 
goal2 = 100 
goal2cel = linecache.getline('storage.txt', 2) 
goal3 = 150 
goal3cel= linecache.getline('storage.txt', 3) 

detectDaniel = 0 


while True: 
    response = requests.get('http://www.bringyourownlaptop.com/php-test') 
    html= response.content 

    soup = BeautifulSoup(html) 
    num = soup.find('section') 
    sub_num = int(num.contents[0].strip()) 

    # figure out if goal have been reached and set the appropriate goal celebration flag 
    if sub_num == goal1 and goal1cel == 0: 
     goal1cel = 1 

    if sub_num == goal2 and goal2cel == 0: 
     goal2cel = 1 

    if sub_num == goal3 and goal3cel == 0: 
     goal3cel = 1 

    # This passed the current status of the goal1cel e.g. 0 not done, 1 current, 2 finished. 
    text_file = open("storage.txt", "w") 
    #This added it to the text document with slash n used as a line break. Weve also had to change the value to a string instead of a integer. 
    text_file.write(str(goal1cel)+"\n"+str(goal2cel)+"\n"+str(goal3cel)) 
    #closes the file. 
    text_file.close() 


    detectDaniel = 1 

    # checks if celebrations need to happen from goal celebration flags 
    if goal1cel == 1 and detectDaniel == 1: 
     GPIO.output(21, True) 
     pygame.mixer.music.play() 
     while pygame.mixer.music.get_busy() == True: 
      continue 
     print("Goal 1 reached!") 
     goal1cel = 2 

    if goal2cel == 1 and detectDaniel == 1: 
     GPIO.output(21, True) 
     pygame.mixer.music.play() 
     while pygame.mixer.music.get_busy() == True: 
         continue 
     print("Goal 2 reached!") 
     goal2cel = 2 

    if goal3cel == 1 and detectDaniel == 1: 
     print("Goal 3 reached!") 
     goal3cel = 2 


    print(sub_num) 
    print(detectDaniel) 
+0

'linecache'我沒有關於相關模塊的任何想法,所有行都得到了「\ n \ r」,在提出'\ r'字符時做了什麼模塊?在單行檢測單個字符(但字符不可讀) – dsgdfg

回答

0

你的錯誤在於假定linecache.getline只會返回一個整數。如果你在解釋器中運行它,它將返回字符串'0\n',這是一個字符串。

當你得到這條線後,你需要找到一些方法來簡單地得到你想要的數字(如果它們只會是1個字符值,你可能只需要第一個字符並與字符進行比較,例如if goal1cel == '1'

+0

感謝您的快速反應凱文。我明白你的意思,但我仍然不確定如何實現它。任何類型的編碼,我只是放慢了這一點。如果你有時間,你能告訴我你將如何寫它?丹 –

+0

因爲所有你應該關心的是字符串的第一個字符,所以你應該可以說'如果goal1cel [0] =='1''(如果該行中的第一個字符是1,這將返回true) 。如果你不熟悉字符串和數組,你可能想閱讀一些Python教程,因爲這些是使用該語言的核心概念,並且需要基礎知識。 –

+0

感謝您的幫助凱文。非常感激。 –