如何在不同的python脚本中将python脚本的输出设置为变量?

2024-05-16 04:17:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用的是Temper1F传感器,我使用的代码库是https://github.com/urwen/temper。这个脾气暴躁代码返回输出,如下所示。你知道吗

$ python3 temper.py
Bus 001 Dev 006 413d:2107 TEMPerX_V3.3 22.6C 72.7F - - - -

我想把这个输出作为一个字符串变量放到另一个python脚本中。我试过使用操作系统。如何调用另一个python脚本并将输出存储到变量?你知道吗

我试过以下方法:

from subprocess import call
var = str(call(["python", "temper.py"]))
var2 = call(["python", "temper.py"])

import os 
os.system('python temper.py')

import temper
var = temper

call方法返回0。你知道吗


Tags: 方法代码pyhttpsimportgithub脚本com
1条回答
网友
1楼 · 发布于 2024-05-16 04:17:37

您是否无法从传感器脚本访问方法?那会使事情简化很多。。。你知道吗

如果没有,你可以用系统标准在脚本中:

import fileinput
import sys

while True:
  line = sys.stdin.readline().strip()
  if line: print "[received]: ", line

并将传感器脚本的输出输入到您的脚本中:

python sensorscript.py | python yourscript.py

[edit]我已经签出了temprese脚本,它看起来很容易直接用作模块:

from temper import Temper
temper = Temper()
value = temper.read() # call this every time you want to read the sensor

相关问题 更多 >