简体   繁体   English

如果在Linux上使用此python脚本,如何获取价值?

[英]How can i get a value out if this python script on linux?

Hey I am trying something on my new raspberry pi and i had to post my question here because I am a noob. 嘿,我正在尝试在新的覆盆子pi上做某事,由于我是菜鸟,因此不得不在此处发布问题。 I am trying to get the temperature of my cpu with the following command : 我正在尝试通过以下命令获取我的CPU的温度:

cmd = "vcgencmd measure_temp | awk -F"=|'" '{print $2}'
Temp = subprocess.check_output(cmd, shell = True )`

When i run vcgencmd measure_temp i get temp= x'C with x = 30/30.5/40.8 etc. I want to get the numbers only so i can make an Ifelse statement like this: 当我运行vcgencmd measure_temp时,我得到temp = x'C,x = 30 / 30.5 / 40.8等。我只想获取数字,这样我就可以做出这样的Ifelse语句:

   If Temp >= 40
   print ("40+")
   elif Temp >=35
   print ("35+")
   else: 
   print ("Below 35")

But I always get this syntaxerror 但是我总是遇到这个语法错误

You have quoting problems in your assignment to cmd . 您对cmd的分配中存在引用问题。 You're using " as the delimiter around the Pythong string, and also around the argument to -F in the shell command. That's ending the Python string. Use single quotes around the shell argument. 您正在使用"作为Pythong字符串的分隔符,还使用了shell命令中-F的参数。这就是Python字符串的结尾。在shell参数周围使用单引号。

cmd = "vcgencmd measure_temp | awk -F'=|' '{print $2}'"

Yuo're also missing colons and indentation in your if statement. 您的if语句中还缺少冒号和缩进。 And Python is case-sensitive, so If should be if . 而且Python区分大小写,因此If应该是if

if Temp >= 40:
    print ("40+")
elif Temp >=35:
    print ("35+")
else: 
    print ("Below 35")

There were a number of issues with your code, such as the usage of " inside your string definition which was closing the string when you didn't want to, as well as the lack of colons and indentation after your if / elif / else statements. (Also just a note, as mentioned in the PEP 8 style guide try not to use variable names like Temp , as it is not one of the commonly distinguished styles) 您的代码有很多问题,例如,在字符串定义中使用"时,您不想使用时会关闭字符串,以及if / elif / else语句后缺少冒号和缩进(也请注意,如PEP 8样式指南中所述,请尽量不要使用像Temp这样的变量名,因为它不是常用的样式之一)

Here's my updated version of your code which should work: 这是我的代码更新后的版本, 应该可以运行:

cmd = "vcgencmd measure_temp | awk -F '=|' '{print $2}'"
temp = subprocess.check_output(cmd, shell = True)

if temp >= 40:
    print("40+")
elif temp >= 35:
    print("35+")
else: 
    print("Below 35")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM