-2

I am trying this simple code to read data from a website, but it gives me KeyError['p']:

for i in range(25200):

    time.sleep(1)
    with requests.Session() as s:
               data = {'current' : 'afghan_usd' }
               r = s.get('http://call5.tgju.org/ajax.json?2019061716-20190617171520-I4OJ3OcWf4gtpzr3JNC5', json = data ).json()
               #print(r)
    for key, value in r["current"].items():
        last_prices = (r[key]['p'])
        z.append(last_prices)
        mid.append(mean(z)) 

The given r is like this:

{'current': {'afghan_usd': {'p': '154530', 'h': '157260', 'l':
 '154530', 'd': '3640', 'dp': 2.36, 'dt': 'low', 't': '۱۷:۲۷:۰۳',
 't_en': '17:27:03', 't-g': '۱۷:۲۷:۰۳', 'ts': '2019-06-17 17:27:03'}}

And you can see the full content of response(r ) here: https://github.com/rezaee/coursera-test/issues/1

EDIT:

I edited my code like this:

for i in range(25200):

    time.sleep(1)
    with requests.Session() as s:
               data = {'current' : 'afghan_usd' }#code}
               r = s.get('http://call5.tgju.org/ajax.json?2019061716-20190617171520-I4OJ3OcWf4gtpzr3JNC5', json = data ).json()
               #print(r)

    for key, value in r["current"]["afghan_usd"].items():
        last_prices = float(value.replace("," , ""))
        z.append(last_prices)
        mid.append(mean(z)) 

But I get this new error:

AttributeError: 'float' object has no attribute 'replace'

Hasani
  • 3,543
  • 14
  • 65
  • 125
  • There is something missing here: `'current': , 'afghan_usd'`, between comma and colon. Are you sure of your output? – olinox14 Jun 17 '19 at 13:33
  • @olinox14: Sorry, I fixed it. The real output has many other object's that I wanted to only copy the specific one. – Hasani Jun 17 '19 at 13:37

3 Answers3

2

I think you're trying to loop over r["current"]:

for key, value in r["current"].items():
    # for first iteration:
    # key is afghan_usd
    # value is {'p': ....}
    try:
        price = value["p"]
    except TypeError:  # value is a string
        price = value
    last_prices = float(price.replace(',', ''))
    z.append(last_prices)
    mid.append(mean(z))
AdamGold
  • 4,941
  • 4
  • 29
  • 47
  • Thank you for help. I am trying to run your code. For your question, because I am trying to plot my chart online. So I use `mean()` inside the loop. – Hasani Jun 17 '19 at 13:48
  • I get this error with your code: `TypeError: can't convert type 'str' to numerator/denominator` – Hasani Jun 17 '19 at 13:51
  • You haven't posted the full response, but in case `p` value is always an integer, you can write `last_prices = int(value['p'])` – AdamGold Jun 17 '19 at 13:52
  • ValueError: invalid literal for int() with base 10: '126,693' – Hasani Jun 17 '19 at 13:59
  • This is the good answer. And the `ValueError` comes from the fact you are trying to get the `mean` of strings. Consider changing `last_prices = (value['p'])` by `last_prices = float(value['p'])` – olinox14 Jun 17 '19 at 14:08
  • You haven't posted an example with comma separated strings. Anyway, try `last_prices = int(value['p'].replace(',', ''))` – AdamGold Jun 17 '19 at 14:15
  • It says `TypeError: string indices must be integers ` . See the edit part I added to my question – Hasani Jun 17 '19 at 14:34
  • You'd have to share the full JSON response in order for us to understand the context of the errors. – AdamGold Jun 17 '19 at 14:37
  • Do you know a place to upload text file? It's a very big string – Hasani Jun 17 '19 at 14:42
  • Dear @AdamGold: I uploaded the full responce(content of `r`) here: https://github.com/rezaee/coursera-test/issues/1 – Hasani Jun 17 '19 at 15:12
  • When you use `last_prices = float(value['p'].replace(',', ''))`, add `print(value)` and post the last printed line before the error. – AdamGold Jun 17 '19 at 15:51
  • Value is a string with the content`133,300` – Hasani Jun 17 '19 at 16:18
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/195074/discussion-between-adamgold-and-user145959). – AdamGold Jun 17 '19 at 16:22
1

A few suggestions:

Move this before the loop

with requests.Session() as s:

And before that line have

data = {'current' : 'afghan_usd' }

Then have your loop and double check that you are accessing at the right level as it looks like this:

last_prices = (r[key]['p'])

is producing an object rather than a simple datatype.

Be sure to indent this properly within your code as it should be within the outer loop

for key, value in r.items():
QHarr
  • 83,427
  • 12
  • 54
  • 101
0

You are looping through r.items() and retrieving "p" from each each. The item "current" doesn't have an entry "p".

Karl
  • 5,573
  • 8
  • 50
  • 73