函数返回字符串而不是字典

2024-10-08 19:19:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个名为“随机词”的软件包,它每天生成一个随机词

这是主程序:

from random_word import RandomWords

r = RandomWords()
word = r.word_of_the_day()
print( word )

这是输出:

'{"word": "daedal", "definations": [{"source": "gcide", "text": "Cunningly or ingeniously formed or working; skillful; artistic; ingenious.", "note": null, "partOfSpeech": "adjective"}, {"source": "gcide", "text": "Crafty; deceitful.", "note": null, "partOfSpeech": "adjective"}]}'

我知道这是一个简单的问题,但是除了使用exec()的页面之外,我可以在网上找到任何东西 返回类型是string,我不能像使用字典一样使用它。我该如何处理这个问题?我尝试使用exec()eval()或转换为list()dict()甚至set(),但都没有效果

这是使用exec()时的代码:

from random_word import RandomWords

r = RandomWords()
word = exec(r.word_of_the_day())

下面是错误消息:

Traceback (most recent call last):
  File "c:\Users\filip\Desktop\test_words.py", line 4, in <module>
    word = exec(r.word_of_the_day())
  File "<string>", line 1, in <module>
NameError: name 'null' is not defined

Tags: orofthetextfromimportsourcerandom

热门问题