Python Json with returns AttributeError: __enter__

2024/9/8 10:24:53

Why does this return AttributeError: __enter__

Sorting method is just a string created based on how the list is sorted, and current time uses 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)

Full traceback:

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

You just for got to use 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)
https://en.xdnf.cn/q/73108.html

Related Q&A

Workflow for adding new columns from Pandas to SQLite tables

SetupTwo tables: schools and students. The index (or keys) in SQLite will be id and time for the students table and school and time for the schools table. My dataset is about something different, but I…

What is the return type of the find_all method in Beautiful Soup?

from bs4 import BeautifulSoup, SoupStrainer from urllib.request import urlopen import pandas as pd import numpy as np import re import csv import ssl import json from googlesearch import search from…

All addresses to go to a single page (catch-all route to a single view) in Python Pyramid

I am trying to alter the Pyramid hello world example so that any request to the Pyramid server serves the same page. i.e. all routes point to the same view. This is what iv got so far: from wsgiref.sim…

Python singleton / object instantiation

Im learning Python and ive been trying to implement a Singleton-type class as a test. The code i have is as follows:_Singleton__instance = Noneclass Singleton:def __init__(self):global __instanceif __i…

Single-Byte XOR Cipher (python)

This is for a modern cryptography class that I am currently taking.The challenge is the cryptopals challenge 3: Single-Byte XOR Cipher, and I am trying to use python 3 to help complete this.I know that…

Basemap Heat error / empty map

I am trying to plot a scattered heat map on a defined geo location. I can very well plot a normal scattered map with no background but I want to combine it with a given lat and lon. I get the following…

Keras custom loss function per tensor group

I am writing a custom loss function that requires calculating ratios of predicted values per group. As a simplified example, here is what my Data and model code looks like: def main():df = pd.DataFrame…

How does numpy.linalg.inv calculate the inverse of an orthogonal matrix?

Im implementing a LinearTransformation class, which inherits from numpy.matrix and uses numpy.matrix.I to calculate the inverse of the transformation matrix.Does anyone know whether numpy checks for or…

pandas: Using color in a scatter plot

I have a pandas dataframe:-------------------------------------- | field_0 | field_1 | field_2 | -------------------------------------- | 0 | 1.5 | 2.9 | -------------------…

Framing Errors in Celery 3.0.1

I recently upgraded to Celery 3.0.1 from 2.3.0 and all the tasks run fine. Unfortunately. Im getting a "Framing Error" exception pretty frequently. Im also running supervisor to restart the t…