Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

basic Python JSON Datetime convert issue

i'm a python beginner and having an issue. Trying to pull API info and convert the extracted JSON time object to a datetime object in Python so I can run the date.weekday() function on it eventually (the overall goal is to extract all the dates from the API and see which the output by days - I plan on populating a empty dictionary once I can extract all the dates).

For some reason, even with my conditional statements, I'm still printing (2015, 04, 06) with all the zeroes. That's my problem.

I have a feeling I'm getting something basic wrong, and that there's also a better way to go about this than doing all the ifs/elses with the 0-padding in the date object.

here's my code so far:

from datetime import date
import datetime
import json
import requests

r = requests.get('https://api.github.com/repos/mbostock/d3/commits?since=2015-04-12330:00:000')
jsonoutput = r.json()
modified = (jsonoutput[0]["commit"]["author"]["date"])
#at this point, modified gives something like: "2015-04-06T22:28:16Z"

if modified[5] == 0:
    if modified[8] == 0:
        new_format = ("(" + modified[0:4] + ", " + modified[6] + ", " + modified[9] + ")")
        #with the particular jsonoutput[0] pulled here, this one should be triggered
    else:
        new_format = ("(" + modified[0:4] + ", " + modified[6] + ", " + modified[8:10] + ")")

else:
    if modified[8] == 0:
        new_format = ("(" + modified[0:4] + ", " + modified[5:7] + ", " + modified[9] + ")")
    else:
        new_format = ("(" + modified[0:4] + ", " + modified[5:7] + ", " + modified[8:10] + ")")

print(new_format)

print(date.weekday(datetime.date(new_format)))
like image 405
SpicyClubSauce Avatar asked Jun 18 '26 09:06

SpicyClubSauce


1 Answers

The error happens in your current code because new_format is defined as a string, while the datetime.date takes the arguments as integers. Also, since you compare the string "0" with the numerical 0, the modified is simply not created.

Instead of this:

new_format = ("(" + modified[0:4] + ", " + modified[6] + ", " + modified[8:10]) + ")")

do this:

new_format = (int(modified[0:4]), int(modified[5:7]), int(modified[8:10]))

The above will work with all of your cases, so you can remove the convoluted if-else blocks as well.

Alternatively, you could split this modeified string by "T", and then use another split by "-" to get the integer values:

new_format = map(int, modified.split("T")[0].split("-"))

You'll also need to unpack the list when passing as the argument, so your complete code becomes:

import json
import requests
from datetime import date

r = requests.get('https://api.github.com/repos/mbostock/d3/commits?since=2015-04-12330:00:000')
jsonoutput = r.json()
modified = jsonoutput[0]["commit"]["author"]["date"]
new_format = (int(modified[0:4]), int(modified[5:7]), int(modified[8:10]))
print(date.weekday(date(*new_format)))

Also, as others have already pointed out in their answers, it might be a better idea to dateutil.parser.parse than to write your own parsing logic. (dateutil is not a builtin package, you'll have to install it) :)

like image 85
Anshul Goyal Avatar answered Jun 20 '26 00:06

Anshul Goyal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!