Python 在打印选定点之前向用户询问日期时间

Python 在打印选定点之前向用户询问日期时间,python,datetime,graph,Python,Datetime,Graph,我使用的是Python2.7.3,在将所选条件绘制到图形之前,如何要求用户输入“from datetime”和“to datetime” 例如,当用户 自日期时间:21/7/2014 0:00 截止时间:22/7/2014 23:57 from datetime import datetime import matplotlib.pyplot as plt import matplotlib.dates as mdates x = [] y = [] t = [] fig = plt.figur

我使用的是Python2.7.3,在将所选条件绘制到图形之前,如何要求用户输入“from datetime”和“to datetime” 例如,当用户 自日期时间:21/7/2014 0:00 截止时间:22/7/2014 23:57

from datetime import datetime
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
x = []
y = []
t = []
fig = plt.figure()
rect = fig.patch
rect.set_facecolor('#31312e')
readFile = open('data.txt', 'r')
sepFile = readFile.read().split('\n')
readFile.close()
for idx, plotPair in enumerate(sepFile):
    if plotPair in '. ':
        # skip. or space
        continue
    if idx > 1:  # to skip the first line
        xAndY = plotPair.split(',')
        time_string = xAndY[0]
        time_string1 = datetime.strptime(time_string, '%d/%m/%Y %H:%M')
        t.append(time_string1)
        y.append(float(xAndY[1]))
ax1 = fig.add_subplot(1, 1, 1, axisbg='white')
ax1.xaxis.set_major_formatter(mdates.DateFormatter('%d/%m/%Y %H:%M'))
ax1.plot(t, y, 'c', linewidth=3.3)
plt.title('IRRADIANCE')
plt.xlabel('TIME')
fig.autofmt_xdate(rotation=45)
fig.tight_layout()
plt.show()
试试这个

#!/usr/bin/env python
#-*- coding:utf-8 -*-

from datetime import datetime
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
x = []
y = []
t = []
fig = plt.figure()
rect = fig.patch
rect.set_facecolor('#31312e')
readFile = open('C:\\Temp\\001.txt', 'r')
sepFile = readFile.read().split('\n')
readFile.close()

startTime = raw_input('please enter start time in format like 21/7/2014 0:00 :')
endTime   = raw_input('please enter end time in format like 22/7/2014 23:57 :') 

# startTime = '21/7/2014 0:02'
# endTime = '22/7/2014 23:58'
startTime = datetime.strptime(startTime, '%d/%m/%Y %H:%M')
endTime = datetime.strptime(endTime, '%d/%m/%Y %H:%M')


for idx, plotPair in enumerate(sepFile):
    if plotPair in '. ':
        # skip. or space
        continue
    if idx > 1:  # to skip the first line
        xAndY = plotPair.split(',')
        time_string = xAndY[0]
        time_string1 = datetime.strptime(time_string, '%d/%m/%Y %H:%M')
        if startTime<=time_string1 <=endTime:
            t.append(time_string1)
            y.append(float(xAndY[1]))
ax1 = fig.add_subplot(1, 1, 1, axisbg='white')
ax1.xaxis.set_major_formatter(mdates.DateFormatter('%d/%m/%Y %H:%M'))
ax1.plot(t, y, 'c', linewidth=3.3)
plt.title('IRRADIANCE')
plt.xlabel('TIME')
fig.autofmt_xdate(rotation=45)
fig.tight_layout()
plt.show()
#/usr/bin/env python
#-*-编码:utf-8-*-
从日期时间导入日期时间
将matplotlib.pyplot作为plt导入
将matplotlib.dates导入为mdates
x=[]
y=[]
t=[]
图=plt.图()
rect=图块
矩形集合面颜色(“#312E”)
readFile=open('C:\\Temp\\001.txt','r')
sepFile=readFile.read().split('\n')
readFile.close()
startTime=原始输入('请以2014年7月21日0:00:00的格式输入开始时间')
结束时间=原始输入('请以22/7/2014 23:57:'等格式输入结束时间')
#开始时间='21/7/2014 0:02'
#结束时间='22/7/2014 23:58'
startTime=datetime.strtime(startTime,'%d/%m/%Y%H:%m')
endTime=datetime.strtime(endTime,“%d/%m/%Y%H:%m”)
对于idx,枚举中的plotPair(sepFile):
如果在“.”中有plotPair:
#跳过。或空间
持续
如果idx>1:#跳过第一行
xAndY=plotPair.split(',')
时间_字符串=xAndY[0]
time_string1=datetime.strtime(time_字符串,'%d/%m/%Y%H:%m')

如果startTimeHi,我又看到你的问题了。你的意思是要求用户输入两个日期时间,然后将它们放在一个图表上?您现在不需要“data.txt”中的数据?您好,谢谢:)用户将输入从什么时间到什么时间,例如。用户将选择2014年7月21日0:00至2014年7月22日23:57,然后图形将仅显示2014年7月21日0:00至2014年7月22日23:57的数据,是的,仍然使用数据中的数据。txt关于
stdat=input(“输入自日期(dd/mm/yyyy)”)
然后简单地解析输入?嗨,这很有效!非常感谢:)但是,当我输入开始时间:2014年7月21日0:00结束时间:2014年7月21日0:05时,图表看起来非常清晰weird@toothberry是的,那么你必须调整你的代码。别忘了确认一下作为回答。:)有没有可能要求用户在图形上而不是在python shell窗口上输入内容?@toothberry我认为没有一种简单的方法可以做到这一点。但它可以做到吗?至少对像我这样的初学者来说?