【发布时间】:2013-05-14 03:00:51
【问题描述】:
我还是 Python 的新手,我一直在使用脚本从我的 Raspberry Pi 获取系统信息,例如 cpu temp 等,并将其导入到谷歌文档电子表格中。我的目标是从输出中提取数字,格式为temp=54.1'C。我只需要这些数字就能够随时间绘制数据...
我正在使用:
import gdata.spreadsheet.service
import os
import subprocess
import re
email = 'myemail@gmail.com'
password = 'mypassword'
spreadsheet_key = 'sjdaf;ljaslfjasljdgasjdflasdjfgkjvja'
worksheet_id = '1'
def temp():
command = "/opt/vc/bin/vcgencmd measure_temp"
proc = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True)
output = proc.stdout.read()
return output
def main():
spr_client = gdata.spreadsheet.service.SpreadsheetsService()
spr_client.email = email
spr_client.password = password
spr_client.ProgrammaticLogin()
dict = {}
dict['temp'] = temp()
entry = spr_client.InsertRow(dict, spreadsheet_key, worksheet_id)
if __name__ == '__main__':
try:
main()
except:
print "Insert Row Failed!"
以上给出了标准结果。我尝试修改 re.findall(),但无法获得正确的位置或条件的正确组合(r、'/d+'、s 和其他东西)以使其仅返回数字 54.1.. . 我基本上以“插入行失败”结束
任何指导将不胜感激。谢谢!
【问题讨论】:
-
当您说“标准结果”时,大概是指
Insert Row Failed!,是吗?在这个特定的 sn-p 中,我没有看到您正在做任何事情来搜索您描述的temp键;你忘记了一些代码吗? -
好吧,按照标准结果,我的意思是第一段中提到的“temp=54.1'C”。当我开始尝试在各种位置和配置中插入 re.findall() 时,我得到了异常......
-
命令是“/opt/vc/bin/vcgencmd measure_temp”,您可以在终端中输入它,它会给出 temp=54.1'C。这个脚本的设置方式,它在我的谷歌文档的一个单元格中给了我那个文本字符串。然后它继续添加一行并在每次运行时打印结果......这就是我想要的。我在 crontab 中设置它,然后在计时器上获取数据......
-
啊,我明白了。所以你需要添加一些处理来从命令行函数返回的字符串中获取值(例如
54.1,尽管它可能是半任意十进制),对吗? -
是的,我相信温度会有一位小数