json.dumps()
for file data PY-W0079f.write(json.dumps(emojis))
can be replaced with json.dump(emojis, f)
25
26 if emojis:
27 with open('emojis.json', 'w') as f:
28 f.write(json.dumps(emojis))29
30if __name__ == "__main__":
31 loop = asyncio.new_event_loop();
The json
module provides two ways to write JSON data: a .dumps()
method that
accepts a JSON string, and a .dump()
method, that works with files directly.
So instead of using json.dumps()
and writing to a file manually, it is
recommended to use json.dump()
directly.
with open('data.json', 'w') as file:
file.write(json.dumps(data)) # Writing to file manually
class Socket:
def write_data(self, data):
self.socket.write(json.dumps(data)) # Writing to socket manually
self.socket.close()
with open('data.json', 'w') as file:
json.dump(data, file) # Directly passing the file object
class Socket:
def write_data(self, data):
json.dump(data, self.socket) # Directly passing the socket object
self.socket.close()