json.dumps()
for file data PY-W0079f.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(
fil.write(json.dumps(cor_articles))
can be replaced with json.dump(cor_articles, fil)
66 print("oof")
67
68 cor_articles.append(cor.title)
69 fil.write(json.dumps(cor_articles))70 fil.close()
71 ch = self.bot.get_channel(946354506214539315)
72
fil.write(json.dumps(self.articles))
can be replaced with json.dump(self.articles, fil)
59 fil = open("corriere.json", "w")
60 if cor.title in cor_articles:
61 print("exists")
62 fil.write(json.dumps(self.articles))63 fil.close()
64
65 else:
fil.write(json.dumps(self.articles))
can be replaced with json.dump(self.articles, fil)
35
36 self.articles.append(entry["title"])
37
38 fil.write(json.dumps(self.articles))39 print("closing...")
40 fil.close()
41
fil.write(json.dumps(self.articles))
can be replaced with json.dump(self.articles, fil)
25 entry = feed.entries[0]
26
27 if entry["title"] in self.articles:
28 fil.write(json.dumps(self.articles))29 fil.close()
30 return
31 print("oof")
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()