Python如何在字符串中找到正确的结果

标签 python temperature

我正在使用 Occidentalis v0.2 并使用 python 脚本调用 modprobe 以从一个 DS18B20 读取温度!

我也是 python 的新手,所以请多多包涵,这是我代码的一小部分:

with open(path, "r") as f:
  contentArray = []
  for line in f:
    contentArray.append (line)
f.close

s = contentArray[0]
if s.find('YES'):
  return contentArray[0]
else:
  return 88

示例:contentArray[0] 可以给出如下结果:

68 01 4b 46 7f ff 0c 10 05 : crc=3e NO

或:

68 01 4b 46 7f ff 08 10 05 : crc=05 YES

如果上面的代码有误,如何找到YES?因为这说明 正确的 CRC。如果是,我实际上想返回 contentArray[1]( 包含正确的温度值)。

最佳答案

已解决,我不得不在代码中进行此更改:

s = contentArray[0]
if s.find('YES') != -1:
  return contentArray[0]
else:
  return 88

关于Python如何在字符串中找到正确的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12400292/

相关文章:

cpu - 在哪里可以找到CPU的 throttle 温度和关机温度?

python - 如何将 python 对象传递给 C 扩展并在其中调用其方法?

python - 添加注释文本会产生整数错误消息

python - 如何在 Windows 7 64 位上安装 PyOpenSSL?

Python : sklearn svm, 提供自定义损失函数

C - 条件总是跳转到 'Else'?

linux - tinkerboard android-/sys/class/thermal/thermal_zone1/temp : No such file or directory

python - 删除除具有特定条件的行之外的所有行

dataset - 哪里有商业可用的按邮政编码按月划分的温度数据集?

Java-开尔文到华氏度的转换方法