gpt4 book ai didi

python - 无法使用 json.dumps() 将字典写入 python 文件

转载 作者:行者123 更新时间:2023-12-01 02:45:26 25 4
gpt4 key购买 nike

我从 JSON 转储中收到响应,并且尝试将其加载到 log.txt 文件中,但这不会打印出任何内容

我的功能

def get_customer_last_action_executed(self):
"""
Get the customer last action executed
By inputing the CustomerID
:return:
"""
payload ={'customerID': self.CustomerID}
if not self.CustomerID:
customer_id = raw_input("Please provide the customer ID:")
self.CustomerID = customer_id
# raise Exception('No customerID provided')

response = self.send_request(self.get_customer_last_action_executed_url + self.CustomerID,
json.dumps(payload),
"GET")

print response.url, response.status_code
print response, response.text, response.reason
if response:
print self.sucessful_msg
else:
print self.error_msg
with open('log.txt', 'w') as f:
json.dumps(payload, f)

最佳答案

您需要使用的是dump()而不是dumps()

json.dump()

Serialize obj as a JSON formatted stream to fp (a .write()-supporting file-like object

If ensure_ascii is False, some chunks written to fp may be unicode instances

json.dumps()

Serialize obj to a JSON formatted str

If ensure_ascii is False, the result may contain non-ASCII characters and the return value may be a unicode instance

完整详细信息可以在 this 上找到线程

关于python - 无法使用 json.dumps() 将字典写入 python 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45301721/

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