datetime format for processing a CSV file [closed]

為{幸葍}努か 提交于 2021-01-29 20:10:49

问题


I am processing a CSV file. So the format in my CSV file is: 2020-10-26T03:45:00Z (example).

How do I read this format to process the entire Excel sheet? for eg: for a date, we can time = datetime.strptimerow[0],'%Y/%d/%m')

How can I use it similarly for my given format? here is the snippet:

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])

format required 2020-10-26T03:45:00Z year,month,day,hour,minutes,zone


回答1:


You can use parse from dateutil package:

from dateutil.parser import parse
parse("2020-10-26T03:45:00Z")

Output:

datetime.datetime(2020, 10, 26, 3, 45, tzinfo=tzutc())


来源:https://stackoverflow.com/questions/65881405/datetime-format-for-processing-a-csv-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!