json.loads()
for file data PY-W0078json.loads(ses.read())
can be replaced with json.load(ses)
8 self.rss_url = "https://www.repubblica.it/rss/esteri/rss2.0.xml"
9 self.corriere_url = "http://xml2.corriereobjects.it/rss/esteri.xml"
10 ses = open("articles.json", "r")
11 self.articles = json.loads(ses.read())12 ses.close()
13 self.send_announcements.start()
14 self.send_announcements_corriere.start()
json.loads(session.read())
can be replaced with json.load(session)
56 print("a")
57 session = open("corriere.json ", "r")
58
59 cor_articles = json.loads(session.read())60 session.close()
61 print("corriere")
62 print(cor.title in cor_articles)
json.loads(f.read())
can be replaced with json.load(f)
24 @commands.command()
25 async def settings(self, ctx, key, value):
26 f = open("settings.json", "r")
27 jsonf = json.loads(f.read()) 28 f.close()
29 jsonf[key] = value
30 f = open("settings.json", "w")
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()