如何在python2.7中打印输出结果
我正在数组中存储一些数据,如温度、湿度和强度,这是我的arduino输出和python2.7的输入,我正在根据这些数据绘制图表。 我还想将arduino输出存储到文本文件中,但由于我是python新手,所以我无法这样做 这是我的python代码如何在python2.7中打印输出结果,python,Python,我正在数组中存储一些数据,如温度、湿度和强度,这是我的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.
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作为输出
非常感谢您的帮助……通过打印,您是指输出到文本文件吗?如果是,您可以添加:
np.savetxt('filename.txt', np.r_[t,h,l])
[filename.txt可以是您希望文本文件的任何名称,除非您有]
np.r将连接矩阵。我不确定您是否打算这样做,但这是一个选项仅使用以下命令打开文件:
f = open('file.txt', 'w')
然后,您可以使用以下命令对其进行写入:
f.write("Value of t : {}".format(t))
最后使用关闭文件
f.close()
你用谷歌搜索过吗?是的,但我无法与我的代码连接。你必须更具体地说明你在将数据存储到文本文件方面遇到的困难,即什么扩展名、什么格式等。。。到目前为止你试过什么?