json.loads()
for file data PY-W0078json.loads(f.read())
can be replaced with json.load(f)
132
133 if includeCategories:
134 with open("ingredients/categories.json", "r") as f:
135 return_data["categories"] = json.loads(f.read())136
137 # ----- CACHE -----
138 # add data to cache
json.loads(f.read())
can be replaced with json.load(f)
102 for ingredient in matching_ingredients:
103 if ingredient.split("/")[0] == category:
104 with open(f"ingredients/{ingredient}", "r") as f:
105 ingredient_data = json.loads(f.read())106
107 ingredient_name = ingredient.split("/")[1].replace(".json", "")
108
json.loads(f.read())
can be replaced with json.load(f)
46
47 for ingredient in ingredients:
48 with open(f"ingredients/{category}/{ingredient}", "r") as f:
49 ingredient_data = json.loads(f.read()) 50
51
52 # ----- STATS -----
The json
module provides two ways to read JSON data: a .loads()
method that
accepts a JSON string, and a .load()
method, that works on files directly.
So instead of reading a file manually and passing it to json.loads()
, it is
recommended to use json.load()
directly.
with open('data.json') as file:
data = json.loads(file.read()) # Reading file manually
class Socket:
def read_json(self, data):
json.loads(self.socket.read()) # Reading socket manually
self.socket.close()
with open('data.json') as file:
data = json.load(file) # Directly passing the file object
class Socket:
def read_json(self, data):
json.load(self.socket) # Directly passing the socket object
self.socket.close()