Python JSON Datetime core problem

I am a python beginner and have a problem. Trying to dump the API information and convert the retrieved JSON time object to a datetime object in Python, so I can run date.weekday () function on it eventually (the overall goal is to fetch all dates from the API and see what the result is by days - I plan to populate an empty dictionary as soon as I can extract all the dates).

For some reason, even with my conditional statements, I am still printing (2015, 04, 06) with all zeros. It is my problem.

I have a feeling that I have something wrong and that there is a better way to do it than doing all ifs / elses with 0-padding on the date object.

here is my code:

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

      

+3


source to share


3 answers


The error is in your current code, because new_format is defined as a string and datetime.date

takes arguments as integers. Also, since you are comparing a string "0"

to a numeric one 0

, the modified is simply not generated.

Instead of this:

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

      

do the following:

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

      

The above will work for all of your cases, so you can remove collapsed blocks if

- else

.



Alternatively, you can split this modified string by "T"

and then use another split by "-"

to get the integer values:

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

      

You will also need to unpack the list when passed as an argument, so your complete code will look like this:

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 noted in others' answers, it would be dateutil.parser.parse

better to write your own parsing logic. (dateutil is not a built-in package, you will have to install it) :)

+2


source


What you get from json is actually an ISO formatted datetime representation



You can refer to this SO answer fooobar.com/questions/17001 / ... to convert the string

0


source


You are trying to create your own parsing functions where Python has its own.

from dateutil import parser
from datetime import date

my_date = dateutil.parser.parse(modified)

is_week_day = date.weekday(my_date)

      

If dateutil

not installed on your computer, trypip install python-dateutil


However, if you want to go with the Python standard library:

from datetime import date, datetime
from time import strptime

mytime = strptime(modified, '%Y-%m-%dT%H:%M:%SZ')
my_date = datetime(*my_time[:6])

is_week_day = date.weekday(my_date)

      

0


source







All Articles