简体   繁体   中英

I am trying to parse information from a JSON URL file using two user inputs from python

This is the code that works successfully by asking a user one input, which in this case, is the state

import requests
import simplejson as json

response = requests.get("https://api.covidtracking.com/v1/states/ca/daily.json")

json_test = response.json()

print("Enter the state for which the COVID data should be retrieved (e.g. TX): ")
user_state = input()


count = 0
for i in json_test:
    i = count
    count = count + 1
    state = (json_test[i]['state'])
    dates = (json_test[i]['date'])
    

    if state == user_state:
            
        death = (json_test[i]['death'])
        print("State: " + str(state))
        print("Date: " + str(dates))
        print("Death(s)" + str(death))
        print("===============")

the output would be something like:

Date: 20201112
Death(s)18108
===============
State: CA
Date: 20201111
Death(s)18070
===============
State: CA
Date: 20201110
Death(s)18001
===============
State: CA
Date: 20201109
Death(s)17977
===============

But I'm trying to implement the option for the user to enter the US state AND date on the command line with a something like:

Enter the state for which the COVID data should be retrieved (e.g. TX): CA
Enter the date for which the COVID data should be retrieved (e.g. 20201219): 20210219

The code I have tried is:

import requests
import simplejson as json

response = requests.get("https://api.covidtracking.com/v1/states/ca/daily.json")

json_test = response.json()

print("Enter the state for which the COVID data should be retrieved (e.g. TX): ")
user_state = input()
print("Enter the date for which the COVID data should be retrieved (e.g. 20201219): ")
user_date = input()

count = 0
for i in json_test:
    i = count
    count = count + 1
    state = (json_test[i]['state'])
    dates = (json_test[i]['date'])
    

    if state == user_state and dates == user_date:
            
        death = (json_test[i]['death'])
        print("State: " + str(state))
        print("Date: " + str(dates))
        print("Death(s)" + str(death))
        print("===============")

However, my output has no results but no errors either. I do now know why it is not working

Here value of dates received from url has datatype of int you need to convert it into string for comparison. This code will work perfectly:

import requests
import simplejson as json

response = requests.get("https://api.covidtracking.com/v1/states/ca/daily.json")

json_test = response.json()


user_state = input("Enter the state for which the COVID data should be retrieved (e.g. TX): ")

user_date = input("Enter the date for which the COVID data should be retrieved (e.g. 20201219): ")

count = 0
for i in json_test:
    i = count
    count = count + 1
    state = (json_test[i]['state'])
    dates = (json_test[i]['date'])


    if((state == user_state)and(str(dates) == user_date)):

        death = (json_test[i]['death'])
        print("State: " + str(state))
        print("Date: " + str(dates))
        print("Death(s): " + str(death))
        print("===============")

Input

Enter the state for which the COVID data should be retrieved (e.g. TX): CA
Enter the date for which the COVID data should be retrieved (e.g. 20201219): 20210305

Output

State: CA
Date: 20210305
Death(s): 53448
===============

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