【问题标题】:how should I print my output result in python2.7我应该如何在 python2.7 中打印我的输出结果
【发布时间】:2018-06-27 23:40:48
【问题描述】:

我在数组中存储了一些数据,例如温度、湿度和强度,这是我的 arduino 输出和我的 python2.7 的输入,我正在根据这些数据绘制图表。 我也想将 arduino 输出存储到文本文件中,但我无法做到这一点,因为我是 python 新手。

这是我的python代码

import serial
import numpy as np
import matplotlib.pyplot as plt
from drawnow import *
l=[]
t = []
h = []
arduinoData = serial.Serial('com3',115200)
plt.ion()
count=0
def makeFig():
    ax1 = plt.subplot(211)
    plt.ylim(0,100)
    plt.title('Temperature and Humidity')
    plt.grid(True)
    plt.ylabel('temp in C')
    plt.plot(t, 'ro-', label='Drgrees Celsius')
    plt.legend(loc='upper left')
    plt2=plt.twinx()
    plt.ylim(0,100)
    plt2.plot(h,'b^-',label='Humidity in %')
    plt2.legend(loc='upper right')
    ax2 = plt.subplot(212, sharex=ax1)
    plt.ylim(0,100)
    plt.grid(True)
    plt.ylabel('Intensity in Lux')
    plt.plot(l, 'ro-', label='Lux')
    plt.legend(loc='upper left')
while True:
    while (arduinoData.inWaiting()==0):
        pass
    arduinoString = arduinoData.readline()
    dataArray = arduinoString.split(',')
    lux = float (dataArray[0] )
    humd = float ( dataArray[1])
    temp = float ( dataArray[2])
    t.append(temp)
    h.append(humd)
    l.append(lux)
    drawnow(makeFig)
    plt.pause(.000001)
    count=count+1
    if(count>60):
        t.pop(0)
        h.pop(0)
        l.pop(0)

我想将 t 、 h 、 l 存储在文本文件中作为输出 帮助将不胜感激....

【问题讨论】:

  • 你用谷歌搜索过吗?
  • 是的,但我无法连接到我的代码
  • 您必须更具体地说明您在将数据存储到文本文件时遇到的问题,即什么扩展名、什么格式等...到目前为止您尝试了什么?

标签: python


【解决方案1】:

只需使用此命令即可打开文件:

f = open('file.txt', 'w')

然后你可以使用:

f.write("Value of t : {}".format(t))

最后用

关闭文件
f.close()

【讨论】:

    【解决方案2】:

    打印是指输出到文本文件吗?如果是这样,您可以添加:

    np.savetxt('filename.txt', np.r_[t,h,l])
    

    [filename.txt 可以是你想要的文本文件的任何名称,除非你有一个]

    np.r 将连接矩阵。我不确定这是否是您打算做的,但这是一个选项

    【讨论】:

      猜你喜欢
      • 2020-04-13
      • 1970-01-01
      • 1970-01-01
      • 2017-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-24
      • 1970-01-01
      相关资源
      最近更新 更多