该程序绘制从MySQL中的数据库获得的传感器的值的图形。我试图仅在来自数据库的值大于25时向我发送电子邮件,但会向我发送错误消息:
TypeError:“ str”和“ int”的实例之间不支持“>”
我的问题是如何创建条件,以便它使用来自数据库的值(“值”)发送邮件。
这是代码:
def update_graph(dark_theme):
dataSQL = []
X = deque(maxlen=10)
Y = deque(maxlen=10)
sql_conn = MySQLdb.connect('localhost', 'root', 'root', 'DB')
cursor = sql_conn.cursor()
cursor.execute("SELECT value, timestamp FROM sensorParser where sensor='TC'")
rows = cursor.fetchall()
for row in rows:
dataSQL.append(list(row))
labels = ['value','timestamp']
df = pd.DataFrame.from_records(dataSQL, columns=labels)
X = df['timestamp']
Y = df['value']
data = plotly.graph_objs.Scatter(
x=list(X),
y=list(Y),
name='TEMP',
mode='lines+markers'
)
if (df['value'] > 25):
email = 'email@gmail.com'
password = 'pass'
send_to_email = 'email2@gmail.com'
subject = 'ALERT'
message = 'Alert'
file_location = 'C:\\Users\\User\\Desktop\\prograpython\\p.jpg'
msg = MIMEMultipart()
msg['From'] = email
msg['To'] = send_to_email
msg['Subject'] = subject
msg.attach(MIMEText(message, 'plain'))
filename = os.path.basename(file_location)
attachment = open(file_location, "rb")
part = MIMEBase('application', 'octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition',
"attachment;filename= %s" % filename)
msg.attach(part)
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(email, password)
text = msg.as_string()
server.sendmail(email, send_to_email, text)
server.quit()
return {
'data': [data],
'layout' : go.Layout(title="TEMPERATURA",
xaxis={'title':'HORA/FECHA'},
yaxis={'title':'°C'})
}
致谢。
答案 0 :(得分:0)
使用从字符串到浮点的转换:
if (float(df['value']) > 25):
答案 1 :(得分:0)
要修复您的错误,您需要将数据库中的字符串转换为整数。试试:
if (int(df['value']) > 25):
答案 2 :(得分:0)
将温度值转换为浮动值:
Y = df['value'].astype(float)