json.loads()
for file data PY-W0078json.loads(f.read())
can be replaced with json.load(f)
9
10def generatePackageSpecifiers():
11 with open("data/insecure.json") as f:
12 database = json.loads(f.read())13 for pkg, all_specs in database.items():
14 for spec in all_specs:
15 if pkg == '$meta':
json.loads(__f.read())
can be replaced with json.load(__f)
12
13with open("data/insecure.json") as __f:
14 try:
15 INSECURE = json.loads(__f.read())16 except ValueError as e:
17 INSECURE = []
18
json.loads(__f.read())
can be replaced with json.load(__f)
19
20with open("data/insecure_full.json") as __f:
21 try:
22 INSECURE_FULL = json.loads(__f.read())23 except ValueError as e:
24 INSECURE_FULL = []
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()