gpt4 book ai didi

python - 使用 Python 将 JSON 转换为文本文件

转载 作者:行者123 更新时间:2023-11-30 22:53:49 26 4
gpt4 key购买 nike

我已经搜索了有关此问题的解决方案,但没有找到我可以理解的解决方案。我是 Python 新手,需要基本帮助来理解为什么收到错误消息:TypeError: is not JSON Serialized。

import requests
import json

r = requests.get("http://api.bls.gov/publicAPI/v2/timeseries/data/LAUCN040010000000005")

with open("C:\...MyPath...\Output.txt", "w") as outfile:
json.dumps(r, outfile)

这是我正在测试的简单代码。我很感激你的帮助。

最佳答案

您不需要将其与 json 相互转换。只需将其保留为文本即可。

import requests

r = requests.get("http://api.bls.gov/publicAPI/v2/timeseries/data/LAUCN040010000000005")

with open("C:\Users\mhoward2\Documents\Python Scripts\Output.txt", "w") as outfile:
outfile.write(r.text)

关于python - 使用 Python 将 JSON 转换为文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38061742/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com