简体   繁体   中英

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

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

What you get from the json is actually a datetime representation in an ISO format

You can refer to this SO answer https://stackoverflow.com/a/15228038/58129 to convert the string

You're trying to make 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 is not installed on your machine, try pip install python-dateutil


However, if you want to go with Python's 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM