1
所以這是我第一次嘗試Python和編程Raspberry Pi。我在Twitter上提到我的小項目是點亮LED。非常簡單,下面顯示的代碼運行良好。我的問題涉及將以前的提及存儲在文本文件中而不是變量。本質上,代碼檢查printed_ids變量,以查看已經看到的tweet.id列表,以防止LED在每次程序重新運行時不斷閃爍。我的計劃是在預定的工作中運行python代碼,但我不希望處於每次重新啓動pi並運行該程序的情況下,該程序必須通過我所有的提及並將每個事件寫入printed_ids變量。 因此,我的想法是將它們寫入文本文件,以便程序在重新啓動後存活。Python - 將變量寫入文本文件
任何想法/建議?
感謝您的幫助。
import sys
import tweepy
import RPi.GPIO as GPIO ## Import GPIO library
import time ## Import 'time' library. Allows use of 'sleep'
GPIO.setmode(GPIO.BOARD) ## Use board pin numbering
CONSUMER_KEY = '******************'
CONSUMER_SECRET = '*****************'
ACCESS_KEY = '**********************'
ACCESS_SECRET = '*********************'
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)
speed = 2
printed_ids = []
while True:
for tweet in api.mentions_timeline():
if tweet.id not in printed_ids:
print "@%s: %s" % (tweet.author.screen_name, tweet.text)
GPIO.setup(7,GPIO.OUT) ## Setup GPIO Pin 7 to OUT
GPIO.output(7,True)## Switch on pin 7
time.sleep(speed)## Wait
GPIO.output(7,False)## Switch off pin 7
f.open('out','w')
f.write(tweet.id)
##printed_ids.append(tweet.id)
GPIO.cleanup()
time.sleep(60) # Wait for 60 seconds.
我的建議是使用[Python的泡菜模塊(http://docs.python.org/2/library/pickle.html),或也許是一個簡單的sqlite數據庫。 – Evert