gpt4 book ai didi

返回 AttributeError : __enter__ 的 Python Json

转载 作者:太空狗 更新时间:2023-10-30 00:53:30 24 4
gpt4 key购买 nike

为什么会返回 AttributeError: __enter__

排序方法只是根据列表的排序方式创建的字符串,当前时间使用stfttime

current_time = strftime("%Y-%m-%d %H-%M-%S", gmtime())

filename = f"Komplett-{str(sorting_method)}-{str(current_time)}.txt"
if not os.path.exists(f'C:/Users/tagp/OneDrive/Dokumenter/Python/{filename}'):
open(str(filename), "w+")
with (filename, "w+") as json_data:
my_list = {}
my_list["products"] = []
for thing in my_products:
my_list["products"].append({
"Product Title":thing.title,
"Price":thing.price,
"Rating":thing.rating,
"Stock":thing.stock
})
json.dump(my_list, json_data, indent = 4)

完整回溯:

Traceback (most recent call last):
File "komplett.py", line 172, in <module>
with (filename, "w") as json_data:
AttributeError: __enter__

最佳答案

你只是为了使用 open

current_time = strftime("%Y-%m-%d %H-%M-%S", gmtime())

filename = f"Komplett-{str(sorting_method)}-{str(current_time)}.txt"
if not os.path.exists(f'C:/Users/tagp/OneDrive/Dokumenter/Python/{filename}'):
open(str(filename), "w+")
with open(filename, "w+") as json_data:
my_list = {}
my_list["products"] = []
for thing in my_products:
my_list["products"].append({
"Product Title":thing.title,
"Price":thing.price,
"Rating":thing.rating,
"Stock":thing.stock
})
json.dump(my_list, json_data, indent = 4)

关于返回 AttributeError : __enter__ 的 Python Json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46273154/

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