0

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

Swaraj
  • 1
  • 2
  • check out [How do I parse an ISO 8601-formatted date?](https://stackoverflow.com/q/127803/10197418), + [hack to parse 'Z'](https://stackoverflow.com/a/62769371/10197418). – FObersteiner Jan 25 '21 at 09:10

1 Answers1

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())
Nicoowr
  • 770
  • 1
  • 10
  • 29
  • No, my friend, I want the correct format, not the date. the date was an example of the format I required. – Swaraj Jan 25 '21 at 10:17
  • Hmm ... I don't understand what output you expect. Please provide an example of input and expected output. – Nicoowr Jan 25 '21 at 11:17
  • Actually i want to process a CSV file., so in the reader function i have all the attributes with float, int datatypes. But for, date and time idk how to combine both ( date and time) into a single variable. – Swaraj Jan 25 '21 at 11:47
  • It's still not clear: as far as I understand, your CSV has rows like: `2020-10-26T03:45:00Z,open,high,low,close` and you want to parse each row as `date,time,open,high,low,close`. Is this correct? If so, you need to explain it clearly in your question, by providing the expected output. Then, if I understood correctly, you can extract date and time from a datetime object to get your expected output. – Nicoowr Jan 25 '21 at 13:21