Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
110 views
in Technique[技术] by (71.8m points)

python - datetime format for processing a CSV file

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...