在python中从CSV获取特定日期和时间的数据

在python中从CSV获取特定日期和时间的数据,python,csv,Python,Csv,我已经编写了一个python程序,它用python获取特定日期的csv数据。我还想获取特定时间的数据,比如特定日期2小时的数据。但问题是,在我的csv文件中,日期和时间存储在不同的列中(第2列表示时间)。我还想将数据绘制在图表上。这是我的密码: from_raw = self.userInputFromRaw.get() from_date = datetime.date(*map(int, from_raw.split('/'))) print ('From date: =

我已经编写了一个python程序,它用python获取特定日期的csv数据。我还想获取特定时间的数据,比如特定日期2小时的数据。但问题是,在我的csv文件中,日期和时间存储在不同的列中(第2列表示时间)。我还想将数据绘制在图表上。这是我的密码:

 from_raw = self.userInputFromRaw.get()
    from_date = datetime.date(*map(int, from_raw.split('/')))
    print ('From date: = ' + str(from_date))
    to_raw = self.userInputToRaw.get()
    to_date = datetime.date(*map(int, to_raw.split('/')))
    in_file = 'C:\\Users\Lala Rushan\Downloads\ARIF Drop Monitoring Final\ARIF Drop Monitoring Final\DataLog.CSV'
    in_file= csv.reader(open(in_file,"r"))

    for line in in_file:
        _dist = line[0]
        try:
            file_date =  datetime.date(*map(int, line[1].split(' ')[1].split('/')))
            if from_date <= file_date <= to_date:
                self.CsvImport(in_file)

        except IndexError:
            pass

我会使用熊猫模块来处理这里几乎所有的事情。这是我要做的

样本数据 代码
您能解释一下您在代码中面临的具体问题吗?提供您收到的任何错误消息。如果您的代码运行但提供了无效的输出,那么请提供一个示例输入和您获得的输出。另外,提供一个预期的输出。确保您已将问题整合在一起,以满足良好的需求。为什么要手动解析并使用csv阅读器?您还可以显示数据中的一些样本行吗?这是一个函数,它获取日期,然后从csv文件中提取数据,如下所示:2 2017/02/17 23:02:31.615我要获取日期从用户处获取时间,然后根据该时间提取数据。我不知道如何才能同时做到这一点。编辑你的问题以添加信息而不是评论我已经编辑了问题。如果你的文件很大怎么办?分块有好的解决方案吗?
2    2017/02/17  23:02:31.615
1    2017/02/17  23:02:36.611
1    2017/02/17  23:02:41.601
2    2017/02/17  23:02:46.748
2    2017/02/17  23:02:51.620
2    2017/02/17  23:02:56.627
1    2017/02/17  23:03:01.617
2    2017/02/17  23:03:06.646
2    2017/02/17  23:03:11.643
2    2017/02/17  23:02:31.615
1    2017/02/17  23:02:36.611
1    2017/02/17  23:02:41.601
2    2017/02/17  23:02:46.748
2    2017/02/17  23:02:51.620
2    2017/02/17  23:02:56.627
1    2017/02/17  23:03:01.617
2    2017/02/17  23:03:06.646
2    2017/02/17  23:03:11.643
import pandas as pd
import matplotlib.pyplot as plt

filename = "dat.csv"
# arguements for pandas.read_csv
kwargs = {
    "sep": "\s+", # specifices that it's a space separeted file
    "names": ["value", "date", "time"], # names the columns
    "parse_dates": [[1,2]], # combine columns 2 and 3 into a datetime col
    "index_col": "date_time", # set the datetime column as the index
    }
# read the csv into a pandas dataframe
df = pd.read_csv(filename, **kwargs)
# select a certain range of times
# pandas is smart about slicing date ranges out of a datetime index
# just write out a slice like below using a standard datetime format
# and pandas will figure out what you want. Convenient.
df = df["2017-02-17 23:02:31":"2017-02-17 23:02:51"]
# plot it using pandas built in wrapper for matplotlib.pyplot
df.plot()
# show the plot
plt.show()