-1

How do I capture the plaintext of a result in wolfram alpha? I tried it out but it didn't work. Here is my code:

app_id = api_key
client = wolframalpha.Client(app_id)
res = client.query("what is the temperature in georgia")
print("The Weather is " + next(res.results).plainText)
engine.say("The Weather is " + next(res.results).plainText)
engine.runAndWait()

but I get this error: AttributeError: plainText

Shadow
  • 17
  • 5
  • aalways put full error message (starting at word "Traceback") in question (not commet) as text (not screenshot). There are other useful information. – furas Dec 26 '20 at 22:34
  • if you use `next()` two times then you get two different results - first and second - but if there is no second result then you may get error. You have to assing first result to variable `item = next(res.results).plainText` and later `print("The Weather is " + item)` and `engine.say("The Weather is " + item)` – furas Dec 26 '20 at 22:38
  • @furas i'm still getting the same error – Shadow Dec 27 '20 at 16:55
  • first I will repeate: always put full error message (starting at word "Traceback") in question (not commen) as text (not screenshot). Ther are other useful information. – furas Dec 27 '20 at 20:14
  • second: first get only `item = next(res.results)` and use `print(item)`, and `print(type(item))` to see what you get. Maybe you get `None` and then you try `None.plainText`- it could seen in full error message. – furas Dec 27 '20 at 20:16
  • I tested this module with own KEY and it seems this module doesn't work even for example query in documentation. Maybe portal WolframAlpha changed something and this module didn't add modifications in code. At this moment I can get data using `requests` and special URLS - which gives me JSON data with answers. – furas Dec 27 '20 at 21:23

1 Answers1

0

I found that it has to be plaintext instead of plainText. At least in example with pod and subpod it works.

Maybe portal WolframAlpha changed this name in results and now module wolfram has to change it in documentation.

But I don't know why it doesn't work with next(res.results).plaintext. maybe problem is different. Maybe it needs other changes in module.

BTW: you can use next(res.results).keys() to see if it has field plaintext

import os
import wolframalpha

APP_ID = "your-app-id"
#APP_ID = os.getenv("WOLFRAMALPHA_TOKEN")
#print(APP_ID)

#query = "temperature in Washington, DC on October 3, 2012"
query = "what is the temperature in georgia"

client = wolframalpha.Client(APP_ID)

res = client.query(query)

for pod in res.pods:
    print('title:', pod.title)
    for sub in pod.subpods:
        print()
        print(sub.plaintext)
    print('\n---\n')

Result:

title: Input interpretation

temperature | center of the State of Georgia

---

title: Result

16 °C
(41 minutes ago)

---

title: History & forecast

| | | 
low: -4 °C
Sat, Dec 26, 7:00am | average high: | 13 °C
average low: | 1 °C | high: 16 °C
Tue, Dec 29, 4:00pm
 | |

---

title: Weather station information

name | KWRB (Robins AFB)
relative position | 13 km W (from center of the State of Georgia)
relative elevation | (comparable to center of the State of Georgia)
local time | 4:36:35 pm EST | Sunday, December 27, 2020
local sunlight | sun is above the horizon
azimuth: 234° (SW) | altitude: 10° ()

---

title: Weather station comparisons

| position | elevation | current temperature
KWRB | 13 km W | 90 m | 16 °C (41 minutes ago)
KMCN | 19 km WNW | 110 m | 16 °C (44 minutes ago)
KDBN | 45 km ESE | 94 m | 16 °C (42 minutes ago)
(sorted by distance and inferred reliability)

---
furas
  • 134,197
  • 12
  • 106
  • 148