【发布时间】:2019-01-09 17:57:52
【问题描述】:
我想把日期时间放到一个数组中,有解决办法吗?我是新手
import csv
from datetime import datetime
date = []
price = []
tdate = []
tprice = []
with open('TSLA.csv', 'r') as csvfile:
csvR = csv.reader(csvfile)
next(csvR) # skipping column names
for i,row in enumerate(csvR):
row_date = datetime.strptime(row[0], "%m/%d/%Y")
date.append(float(row_date))
price.append(float(row[5]))
如果你想查看错误:
File "csvtest.py", line 14, in <module>
date.append(float(row_date))
TypeError: float() argument must be a string or a number, not 'datetime.datetime'
更新
with open('TSLA.csv', 'r') as csvfile:
csvR = csv.reader(csvfile)
next(csvR) # skipping column names
for i,row in enumerate(csvR):
ts = time.strptime(row[0], "%m/%d/%Y")
time.mktime(ts)
date.append(float(ts))
price.append(float(row[5]))
错误:
TypeError: float() argument must be a string or a number, not 'time.struct_time'
【问题讨论】:
-
当您将日期时间转换为浮点数时,您期望输出什么?
-
我想将日期放入 matplotlib.pyplot 来绘制图表,我该怎么做?因为像 20171140 这样的格式不是连续的