I'm trying to remove duplicates from a list, before I write to a JSON file. I commented the lines where I implemented the code and added extra print statements for debugging. Based on my debugging the code does not get to the print statements and does not write to the JSON file either. My error lies within the function trendingBot(). Currently as the code stands with uncommenting anything, the duplicates will be written to the JSON file.
convertToJson(quote_name, quote_price, quote_volume, url)
quotesArr = []
# Convert to a JSON file
def convertToJson(quote_name, quote_price, quote_volume, url):
quoteObject = {
"url": url,
"Name": quote_name,
"Price": quote_price,
"Volume": quote_volume
}
quotesArr.append(quoteObject)
def trendingBot(url, browser):
browser.get(url)
trending = getTrendingQuotes(browser)
for trend in trending:
getStockDetails(trend, browser)
# requests finished, write json to file
# REMOVE ANY DUPLICATE url from the list, then write json to file.
quotesArr_dict = {quote['url']: quote for quote in quotesArr}
# print(quotesArr_dict)
quotesArr = list(quotesArr_dict.values())
# print(quotesArr)
with open('trendingQuoteData.json', 'w') as outfile:
json.dump(quotesArr, outfile)
Json file with duplicated entries
[
{
"url": "https://web.tmxmoney.com/quote.php?qm_symbol=ACB&locale=EN",
"Volume": "Volume:\n12,915,903",
"Price": "$ 7.67",
"Name": "Aurora Cannabis Inc."
},
{
"url": "https://web.tmxmoney.com/quote.php?qm_symbol=HNL&locale=EN",
"Volume": "Volume:\n548,038",
"Price": "$ 1.60",
"Name": "Horizon North Logistics Inc."
},
{
"url": "https://web.tmxmoney.com/quote.php?qm_symbol=ACB&locale=EN",
"Volume": "Volume:\n12,915,903",
"Price": "$ 7.67",
"Name": "Aurora Cannabis Inc."
}
]