Forum >> Principianti >> Filtra un dizionario per chiavi

Pagina: 1

Voglio importare articoli da tutte le fonti in tutto il mondo a partire da una certa data.



import requests
url = ('https://newsapi.org/v2/top-headlines?'
'country=us&'
'apiKey=de9e19b7547e44c4983ad761c104278f')
response = requests.get(url)

response_dataframe = pd.DataFrame(response.json())

articles = {article for article in response_dataframe['articles'] if article['publishedAt'] == '2019-01-04T11:30:00Z'}
print(articles)


Ma ottengo :



---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-84-0f21f2f50907> in <module>
2 response_dataframe['articles']1['publishedAt']
3
----> 4 articles = {article for article in response_dataframe['articles'] if article['publishedAt'] >= '2018-01-04T11:30:00Z'}
5 print(articles)

<ipython-input-84-0f21f2f50907> in <setcomp>(.0)
2 response_dataframe['articles']1['publishedAt']
3
----> 4 articles = {article for article in response_dataframe['articles'] if article['publishedAt'] >= '2018-01-04T11:30:00Z'}
5 print(articles)

TypeError: unhashable type: 'dict'






Le dict comprehension non si fanno così. https://www.python.org/dev/peps/pep-0274/





Pagina: 1



Esegui il login per scrivere una risposta.