我正在尝试从Raspberry pi创建温度和湿度数据的分析代码。 我的代码似乎没有工作,因为我继续收到此错误
回溯(最近一次呼叫最后):文件" script_name.py",第19行,在 hour = str(columns [2])IndexError:元组索引超出范围pi @ raspberrypi~ $
这是我的代码:
import matplotlib.pyplot as plt
f = open('my_data.txt','r')
file_contents = f.read()
f.close()
line_by_line = file_contents.split('\n')
data = line_by_line[4::]
print data
time = []
temperature = []
humidity = []
for line in data[:-1]:
columns = line.split(':'),(',')
month = str(columns[0])
day = str(columns[1])
hour = str(columns[2])
min = str(columns[3])
sec = str(columns[4])
time_in_seconds = month*(60.0*60.0*24.0*30.0)+day*(60.0*60.0*24.0)+hour* (60.0*60.0)+min*60.0+sec
time.append(time_in_seconds)
humidity.append(columns[5])
temperature.append(columns[6])
import numpy as np
humidity = np.array(humidity)
temperature = np.array(temperature)
missing_data_locations = np.where(humidity == 'None')
missing_data_locations = np.where(temperature == 'None')
humidity[missing_data_locations] = '-999'
temperature[missing_data_locations] = '-999'
humidity = humidity.astype(np.float)
temperature = temperature.astype(np.float)
missing_data_locations2 = np.where(humidity == -999)
missing_data_locations2 = np.where(temperature == -999)
humidity[missing_data_locations2] = np.nan
temperature[missing_data_locations2] = np.nan
print time
print temperature
print humidity
plt.close('all')
fig = plt.figure()
ax1 = fig.add_subplot(211)
ax1.plot(time,temperature,'r')
ax1.set_ylabel('temperature (deg. C)')
ax2 = fig.add_subplot(212)
ax2.plot(time,humidity,'b')
ax2.set_ylabel('humidity (%)')
ax2.set_xlabel('time (seconds)')
plt.savefig('/home/pi/my_fig.png')
plt.show()