简体   繁体   English

如何从 csv 获取日期列表(作为字符串)并仅返回开始日期和结束日期之间的日期/数据?

[英]How can I take list of Dates from csv (as strings) and return only the dates/data between a start date and end date?

I have a csv file with dates in format M/D/YYYY from 1948 to 2017. I'm able to plot other columns/lists associated with each date by list index.我有一个 csv 文件,其日期格式为 M/D/YYYY 从 1948 年到 2017 年。我可以通过列表索引 plot 与每个日期关联的其他列/列表。 I want to be able to ask the user for a start date, and an end date, then return/plot the data from only within that period.我希望能够向用户询问开始日期和结束日期,然后仅在该期间返回/绘制数据。

Problem is, reading dates in from the csv, they are strings so I cannot use if date[x] >= startDate && date[x] <= endDate because theres no way for me to turn dates in this format to integers.问题是,从 csv 读取日期,它们是字符串,所以我不能使用if date[x] >= startDate && date[x] <= endDate因为我无法将这种格式的日期转换为整数。

Here is my csv file 这是我的 csv 文件

I am already able to read in the dates from the csv to its own list.我已经能够阅读从 csv 到它自己的列表的日期。

How can I take the dates in my list and only return the ones within the user specified date range?如何获取列表中的日期并仅返回用户指定日期范围内的日期?

Here is my function for plotting the entire dataset right now:这是我现在绘制整个数据集的 function:

#CSV Plotting function
def CSV_Plot (data,header,column1,column2):

  #pyplot.plot([item[column1] for item in data] , [item[column2] for item in data])
  pyplot.scatter([item[column1] for item in data] , [item[column2] for item in data])
  pyplot.xlabel(header[column1])
  pyplot.ylabel(header[column2])
  pyplot.show()

  return True

CSV_Plot(mycsvdata,data_header,dateIndex,rainIndex)

This is how I am asking the user to input the start and end dates:这就是我要求用户输入开始日期和结束日期的方式:

 #Ask user for start date in M/D/YYY format
  startDate = input('Please provide the start date (M/D/YYYY) of the period for the data you would like to plot: ')
  endDate = input('Please provide the end date (M/D/YYYY) of the period for the data you would like to plot: ')

You need to compare the dates.您需要比较日期。

I would suggest parsing the dates from your CSV into a datetime object, and also turning the user input value into a datetime object.我建议将 CSV 中的日期解析为datetime时间 object,并将用户输入值转换为datetime时间 object。

How to create a datetime object from a string?如何从字符串创建日期时间 object? You need to specify the format string and the strptime() will parse it for you.您需要指定格式字符串, strptime()将为您解析它。 Details here: Converting string into datetime此处的详细信息: 将字符串转换为日期时间

In your case, it could be something like在您的情况下,它可能类似于

from datetime import datetime

# Considering date is in M/D/YYYY format
datetime_object1 = datetime.strptime(date_string, "%m/%d/%Y")

Then you can compare them with a > or < operator.然后您可以将它们与><运算符进行比较。 Here you can find details of how to compare the dates . 在这里您可以找到有关如何比较日期的详细信息

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python。 如何从日期列表中获取开始和结束日期 - Python. How to get start and end date from list of dates 使用 python 从 csv 获取指定开始和结束日期之间的日期范围 - Get range of dates between specified start and end date from csv using python 如何检查元组之间的日期,日期为开始和结束 - How to check the date in between the tuple, dates as start and end 如何通过在 gitlab CICD 中设置环境变量(截止日期、起始日期和项目列表)从 CSV 文件中获取两个日期之间的记录? - How can I get records between two dates from CSV file by setting an environment variable (To date, From Date and List of project) in gitlab CICD? 如何在 Python 中从开始日期和结束日期创建日期列表? - How can I create a list of dates from start and end dates in Python? 使用两个数据框比较开始日期和结束日期之间的日期 - Compare dates between start date and end date with two dataframe 如何使用python查找两个日期之间的日期(开始日期和结束日期除外) - How to find dates between two dates excluding start and end date using python 我使用 Pandas 掩码,以便通过使用输入日期从数据框中获取数据,但未显示开始日期的日期 - I use Pandas mask in order to take a data from dataframe by using input dates but start date's date is not appear 如何仅从数据集中获取第一个日期或合并来自相同日期的数据 - How to take only first date from dataset OR merge data from same dates 用介于两者之间的日期填充开始/结束日期列表 - Fill list of start/end dates with dates in between
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM