我有1500行数据的列表,看起来像
USGS \ t 14211720 \ t 2017-02-12 22:00 \ t PST \ t 117000 \ t P 11.96 P
在我添加datetime行之前,dischargelist和gagheightList工作并给我一个1400行的计数但是当我添加日期时间时我得到了
TypeError: strptime() argument 1 must be str, not list"
I am trying to pull the date, discharge and height to make two graphs
我的代码:
import csv
import datetime
dateList = []
dischargeList = []
gageheightList = []
readCSV = csv.reader(dataLines, delimiter = '\t')
for row in readCSV:
date_str = [2]
dateTIME = datetime.datetime.strptime(date_str,"%Y,%m,%d T%H:%M")
dateList.append(dateTIME)
discharge = row[4]
dischargeList.append(discharge)
gageheight = row[6]
gageheightList.append(gageheight)
#print(dateList)
#this line was just to test if the dates were making it to a list
print("we have {} items in the date list".format(len(dateList)))
print("we have {} items in thegage height list".format(len(gageheightList)))
print ("we have {} items in the discharge list".format(len(dischargeList)))
答案 0 :(得分:1)
正如AJPennster所说,你应该知道如何使用datetime
模块。以下代码是方法datetime.datetime.strptime(string, format)
import datetime
_str1 = "2017,02,12 22:00"
_str2 = "2017-02-12 22:00"
format_str1 = datetime.datetime.strptime(_str1, "%Y,%m,%d %H:%M")
format_str2 = datetime.datetime.strptime(_str1, "%Y-%m-%d %H:%M")
使用datetime.datetime.strptime(string, format)
方法时,您应该严格按照您提供的srting格式填写格式参数。
答案 1 :(得分:0)
date_str = [2]
将date_str
设置为包含2
的列表。
这是你尝试做的原因
dateTIME = datetime.datetime.strptime(date_str,"%Y,%m,%d T%H:%M")
你得到了
TypeError: strptime() argument 1 must be str, not list"
将date_str = [2]
更改为date_str = row[2]
可以解决您的问题。