json.dumps()
for file data PY-W0079fil.write(json.dumps(cor_articles))
can be replaced with json.dump(cor_articles, fil)
70 print("oof")
71
72 cor_articles.append(cor.title)
73 fil.write(json.dumps(cor_articles))74 fil.close()
75 ch = self.bot.get_channel(946354506214539315)
76
fil.write(json.dumps(self.articles))
can be replaced with json.dump(self.articles, fil)
63 fil = open("corriere.json", "w")
64 if cor.title in cor_articles:
65 print("exists")
66 fil.write(json.dumps(self.articles))67 fil.close()
68
69 else:
fil.write(json.dumps(self.articles))
can be replaced with json.dump(self.articles, fil)
28
29 if entry["title"] in self.articles:
30
31 fil.write(json.dumps(self.articles))32 fil.close()
33 return
34 print("oof")
fil.write(json.dumps(self.articles))
can be replaced with json.dump(self.articles, fil)
38
39 self.articles.append(entry["title"])
40
41 fil.write(json.dumps(self.articles))42 print("closing...")
43 fil.close()
44
f.write(json.dumps(jsonf))
can be replaced with json.dump(jsonf, f)
28 f.close()
29 jsonf[key] = value
30 f = open("settings.json", "w")
31 f.write(json.dumps(jsonf)) 32 await ctx.send("updated settings")
33
34 @commands.command(
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()