目标项目在Python库中

huangapple go评论61阅读模式
英文:

Target item in python library

问题

我正在尝试使用psutil在我的树莓派上使用Python仅打印输出中的当前温度。对于修改后的输入的任何帮助将不胜感激。

输入

import psutil

print(psutil.sensors_temperatures())

输出

{'cpu_thermal': [shwtemp(label='', current=36.998, high=None, critical=None)]}

我已经尝试了几种修改输入语句的方法,但没有得到所需的输出:

psutil.sensors_temperatures().current

psutil.sensors_temperatures().cpu_thermal.current

英文:

I am trying to print just the current temperature only from the output using psutil on my raspberry pi with python.
Any help in altered inputs would be greatfully appreciated.

Input

Import psutil

print ((psutil.sensors_temperatures())

Output

{'cpu_thermal': [shwtemp(label='', current=36.998, high=None, critical=None)]}

I have tried several alteration to the input statement but not getting the output required

psutil.sensors_temperatures().current

psutil.sensors_temperatures().cpu_thermal.current

答案1

得分: 0

你来自另一种编程语言。 Python 有所不同:

psutil.sensors_temperatures() 返回一个字典,可以使用语法 dict["key"] 访问。尝试一下:

import psutil
print(psutil.sensors_temperatures()["cpu_thermal"].current)

有关 Python 字典的更多信息,请参阅:https://www.w3schools.com/python/python_dictionaries.asp

英文:

You came from another programming language. Python is different:

psutil.sensors_temperatures() Returns a dictionary, which is accessed with the syntax dict["key"]. Try this:

import psutil
print(psutil.sensors_temperatures()["cpu_thermal"].current)

For more on Python's dicts see: https://www.w3schools.com/python/python_dictionaries.asp

答案2

得分: 0

# 获取温度
cpu_temp = psutil.sensors_temperatures()["cpu_thermal"][0]
print(cpu_temp.current)
英文:
# get the temperature
cpu_temp = psutil.sensors_temperatures()["cpu_thermal"][0]
print(cpu_temp.current)

huangapple
  • 本文由 发表于 2023年5月30日 02:11:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76359514.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定