json - How do i save this python code to text file.?

Closed. This question is off-topic. It is not currently accepting answers.Your question should be specific to WordPress.

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 5 years ago.

Improve this question
# Dependencies
import json
import requests 

# Specify the URL
url = ""

# Make request and store response
r = requests.get(url)

print("Status code:", r.status_code)

response_dict = r.json()



keys = {"setup", "punchline"}
for item in response_dict:
    for key, value in item.items():
          if key in keys:
              print(key, " : ", value)
Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 5 years ago.

Improve this question
# Dependencies
import json
import requests 

# Specify the URL
url = "https://official-joke-api.appspot/jokes/programming/ten"

# Make request and store response
r = requests.get(url)

print("Status code:", r.status_code)

response_dict = r.json()



keys = {"setup", "punchline"}
for item in response_dict:
    for key, value in item.items():
          if key in keys:
              print(key, " : ", value)
Share Improve this question asked Jan 5, 2020 at 0:16 Paa AmoakoPaa Amoako 1 1
  • Sorry, but you just posted a wall of code. Can you explain (by editing the question itself): What is it you're trying to achieve? What have you tried so far? Where exactly are you having problems? And please keep in mind, this community is specific to WordPress. So try to answer all these questions in regard to (the latest) WP please. – kero Commented Jan 5, 2020 at 2:47
Add a comment  | 

1 Answer 1

Reset to default 1

It's off-topic but if you just want to write the setup and punchline values to a text file you can do something like this:

# Dependencies
import json
import requests 

# Specify the URL
url = "https://official-joke-api.appspot/jokes/programming/ten"

# Make request and store response
r = requests.get(url)

print("Status code:", r.status_code)

jokes = json.loads(r.text)

with open("outfile.txt", "w") as f:
    for joke in jokes:
        f.write("{}:{}\n".format(joke["setup"], joke["punchline"]))

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744859522a4597614.html

相关推荐

  • json - How do i save this python code to text file.?

    Closed. This question is off-topic. It is not currently accepting answers.Your question should be specific to WordPress.

    2天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信