我用Python和Pyramid创建了一个Web应用程序,该应用程序从gmail获取最新的电子邮件,将数据保存到文件中,然后使用matplotlib绘制数据,并使用mpld3将其转换为html字符串。目前,我的电子邮件中包含以下格式的数据:
17 25
18 25
19 25
20 26
在绘制此数据时我没有任何问题。但是,我想以相同的形式绘制数据,但带有时间戳,如下所示:
25-06-2018 9:11 25
25-06-2018 9:12 25
25-06-2018 9:13 25
...
我尝试将数据转换为日期时间格式,但没有成功。这是我的代码:
from pyramid.config import Configurator
from pyramid.response import Response
import email
import getpass
import imaplib
import os
import re
import numpy as np
import matplotlib.pyplot as plt, mpld3
def imaptest(request):
m = imaplib.IMAP4_SSL('imap.gmail.com')
m.login('email@gmail.com', 'password')
m.list()
m.select('inbox')
result, data = m.uid('search', None, 'UNSEEN')
i = len(data[0].split()) #space separate string
for x in range(i):
latest_email_uid = data[0].split()[x]
result, email_data = m.uid('fetch', latest_email_uid, '(RFC822)')
raw_email = email_data[0][1]
raw_email_string = raw_email.decode('utf-8')
email_message = email.message_from_string(raw_email_string)
for part in email_message.walk():
if part.get_content_type() == 'text/plain':
body = part.get_payload(decode=True)
save_string = str('C:\Email\mail1' + str(x) + '.txt')
myfile = open(save_string, 'a')
myfile.write(str(body))
myfile.close
else:
continue
with open('C:\Email\mail10.txt') as f:
lines = f.readlines()
x = [int(line.split()[0]) for line in lines]
y = [int(line.split()[1]) for line in lines]
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.set_title('Test Graph')
ax1.set_xlabel('Time')
ax1.set_ylabel('Temperature')
plt.plot(x,y)
# Print as HTML
return Response(
mpld3.show()
)
config = Configurator()
config.add_route('imaptest', '/imaptest')
config.add_route('landingpage', '/landingpage')
config.add_view(landingpage, route_name='landingpage')
config.add_view(imaptest, route_name='imaptest')
app = config.make_wsgi_app()
答案 0 :(得分:1)
如果您都需要日期和时间,那么https://stackoverflow.com/a/51037906/10739643答案是最好的。 如果某人仅需要日期该怎么办,那么您只需添加以下内容:
date = [datetime.strftime(datetime.strptime(str(y[0]),'%Y-%m-%d'),'%Y-%m-%d') for x in dates]
完整格式为:
import from datetime import datetime
with open('file.txt') as f:
lines = f.readlines()
dates = [str(line.split()[0]) for line in lines]
date = [datetime.strftime(datetime.strptime(str(y[0]),'%Y-%m-%d'),'%Y-%m-%d') for x in dates]
答案 1 :(得分:0)
过一会儿我就知道了。
首先,我必须将时间值转换为字符串数组而不是int数组:
lines = f.readlines()
dates = [str(line.split()[0]) for line in lines]
然后我使用datetime模块将数组转换为datetime类型的数组:
from datetime import datetime
date = [datetime.strptime(x,'%d-%m-%Y/%H:%M') for x in dates]
然后我用matplotlib在X轴上绘制日期时间,在Y轴上绘制我的值:
plt.subplot(2,1,1)
plt.title('Temperature & Humidity')
plt.ylabel('Temperature' + u'\u2103')
plt.plot_date(date, y, 'r.-')
这里是整个代码,以防万一其他人可以使用它:
import numpy as np
import matplotlib.pyplot as plt, mpld3
from datetime import datetime
def landingpage(request):
with open('C:\Email\mail10.txt') as f:
lines = f.readlines()
dates = [str(line.split()[0]) for line in lines]
y = [float(line.split()[1]) for line in lines]
z = [float(line.split()[2]) for line in lines]
date = [datetime.strptime(x,'%d-%m-%Y/%H:%M') for x in dates]
plt.subplot(2,1,1)
plt.title('Temperature & Humidity')
plt.ylabel('Temperature' + u'\u2103')
plt.plot_date(date, y, 'r.-')
plt.subplot(2,1,2)
plt.plot_date(date, z, 'b.-')
plt.xlabel('Time')
plt.ylabel('Humidity RH%'.decode('unicode-escape'))
# Print as HTML
return Response(
mpld3.show()
)