Hello I am reading and save a dict of dicts in JSON format, but when I use the json load I get this error.I still need to figure out what problem is it, what’s the problem there? Thank you!!
JSON example:
data = {'multiplayer.it': {'news1.it': (title,date), 'news2.it': (title,date)}, 'site2.it':{'news2.it':(title,date), 'news3.it': (title,date)}}
@tasks.loop(minutes=30) async def get_gamesnews(): sites = ['https://multiplayer.it/articoli/notizie/'] for site in sites: async with aiohttp.ClientSession() as ses: async with ses.get(site) as response: if response.status == 200: text = await response.text() soup = BeautifulSoup(text, 'lxml') if site == 'https://multiplayer.it/articoli/notizie/': div_news = soup.find_all('div', class_='media-body') for news in div_news: titles = news.find_all('a', class_='text-decoration-none') for title in titles: title_news = title.text.strip() link_news = 'https://multiplayer.it' + title['href'] with open('dictionary_news.json', 'r+') as f: dict_news = json.load(f) dictvalues_news = dict_news.get('multiplayer.it') if link_news not in dictvalues_news: date_news = datetime.date.today().strftime('%Y-%m-%d') dict_news['multiplayer.it'][link_news] = (title_news, date_news) print((title_news, link_news, date_news)) channel = client.get_channel(855220263917191228) await channel.send(f'{title_news} {link_news}') json.dump(dict_news, f)
That’s how I create the json file:
import json data = {'multiplayer.it': {}} with open('dictionary_news.json', 'w') as fp: json.dump(data, fp)
Traceback:
Unhandled exception in internal background task 'get_gamesnews'. Traceback (most recent call last): File "C:UsersThundAppDataLocalProgramsPythonPython39libsite-packagesdiscordexttasks__init__.py", line 101, in _loop await self.coro(*args, **kwargs) File "C:UsersThundDesktopRepoBitbucketDiscordBotmain.py", line 75, in get_gamesnews dict_news = json.load(f) File "C:UsersThundAppDataLocalProgramsPythonPython39libjson__init__.py", line 293, in load return loads(fp.read(), File "C:UsersThundAppDataLocalProgramsPythonPython39libjson__init__.py", line 346, in loads return _default_decoder.decode(s) File "C:UsersThundAppDataLocalProgramsPythonPython39libjsondecoder.py", line 337, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "C:UsersThundAppDataLocalProgramsPythonPython39libjsondecoder.py", line 355, in raw_decode raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Advertisement
Answer
The problem is that you’re running json.dump(), after which json.load() can’t be executed any more without reopening the file (at least to my knowledge, there might be workarounds).
Also remember that “r+” open the file in append mode, so that code would probably cause other problems latter.
I would recommend changing the code like this:
with open('dictionary_news.json', 'r') as f: dict_news = json.load(f) for title in titles: title_news = title.text.strip() link_news = 'https://multiplayer.it' + title['href'] dictvalues_news = dict_news.get('multiplayer.it') if link_news not in dictvalues_news: date_news = datetime.date.today().strftime('%Y-%m-%d') dict_news['multiplayer.it'][link_news] = (title_news, date_news) print((title_news, link_news, date_news)) channel = client.get_channel(855220263917191228) await channel.send(f'{title_news} {link_news}') with open('dictionary_news.json', 'w') as f: json.dump(dict_news, f)
Since json.dump() writes the entire json, you should use mode ‘w’ for writing to the file.