2014-12-13 14 views
0

直到本月,我的戶外DHT22溫度/溼度監視器一直工作正常。然而,隨着寒冷的天氣和溫度現在爲零,我注意到我的例行程序無法處理負溫度 - 相反,它們被表示爲積極的:標誌已經丟失。在Python中顯示簽名的花車

我已經調整了re.search例程以包括其中以前它們被排除負數,

# Continuously append data 
while(True): 
# Run the DHT program to get the humidity and temperature readings! 
# DHT22 (Credit to Adafruit) 
output = subprocess.check_output(["/home/pi/scripts/DHT/Adafruit_DHT", "22", "25"]); 
rasp = subprocess.check_output(["vcgencmd", "measure_temp"]) 
print output 
matches = re.search("Temp =\s+-([0-9.]+)", output) 
if (not matches): 
     time.sleep(3) 
     continue 
tempa = float(matches.group(1)) 
print tempa 

當我

print output 

字符串產生包含負溫度。

然而,當我

print tempa 

它顯示爲正數。

我需要能夠將符號帶入變量,因爲溫度可能是正值或負值(即使在英國的冬季)。

任何人都可以幫忙嗎?

回答

2

負號不會在匹配組中捕獲,因爲目前它不是其中的一部分。

"Temp =\s+(-[0-9.]+)" 
     ^fix 
0

我剛剛試了一下,只有「 - 」號是不夠的。如果你想測量正面和負面的溫度下,正則表達式的字符串應該是這樣的:

"Temp =\s+([-+]?[0-9.]+)"