问题描述
我正在处理一个 CSV 文件。所以我的 CSV 文件中的格式是:2020-10-26T03:45:00Z
(示例)。
如何读取这种格式来处理整个 Excel 工作表?
例如:对于一个日期,我们可以time = datetime.strptimerow[0],'%Y/%d/%m')
对于给定的格式,我如何以类似的方式使用它? 这是片段:
with open('nifty.csv') as database:
file = open('nifty.csv',newline='')
reader = csv.reader(file)
header = next(reader)
data = []
for row in reader:
# row = [time,open,high,low,close]
time = datetime.strptime(row[0],'%Y-%d-%m') ##here is where i get
stuck coz i need the right format according to the example given
above.##
open_price = float(row[1])
high = float(row[2])
low = float(row[3])
close = float(row[4])
需要格式 2020-10-26T03:45:00Z 年、月、日、小时、分钟、区域
解决方法
您可以使用 parse
包中的 dateutil
:
from dateutil.parser import parse
parse("2020-10-26T03:45:00Z")
输出:
datetime.datetime(2020,10,26,3,45,tzinfo=tzutc())