json.loads()
for file data PY-W0078json.loads(data)
can be replaced with json.load(open(path + '/%s' % item[0]))
375 for item in IMPORT_SERIES:
376 item[1].is_importing = True
377 data = open(path + '/%s' % item[0]).read()
378 dic = json.loads(data)379 changed_ids = create_service_from_json(
380 task_handle, dic, item, new_event.id, service_ids
381 )
json.loads(open(path + '/event').read())
can be replaced with json.load(open(path + '/event'))
351 # create event
352 try:
353 update_state(task_handle, 'Importing event core')
354 data = json.loads(open(path + '/event').read())355 _, data = _trim_id(data)
356 srv = ('event', Event)
357 data = _delete_fields(srv, data)
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()