Outils pour utilisateurs

Outils du site


issue146:python

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentesRévision précédente
Prochaine révisionLes deux révisions suivantes
issue146:python [2019/07/12 12:45] d52frissue146:python [2019/07/12 12:57] d52fr
Ligne 181: Ligne 181:
        
    print("   Precip Type: {0}".format(response['currently']['precipType']))**    print("   Precip Type: {0}".format(response['currently']['precipType']))**
 +   
 +Et notre sortie ressemblera, en utilisant les données de réponse ci-dessus, à quelque chose comme :
 +
 +   Temperature: 84.43
 +   Feels like: 90.54
 +   Dew point: 72.43
 +   Humidity: 67.0%
 +
 +Ça n'est pas aussi mauvais qu'il y paraissait au début de cet article, n'est-ce-pas ? Maintenant, comment manipuler un champ de sorte que :
 +••s'il est disponible, il est présenté,
 +••s'il ne l'est pas, qu'il n'y ait pas d'erreur ?
 +
 +Nous utiliserons la clé precipType comme exemple. S'il pleut ou neige, il sera disponible, mais si le soleil brille, il ne sera pas présent dans les données. Voici comment :
 +
 +if 'precipType' in response['currently']:
 +   
 +   print("   Precip Type: {0}".format(response['currently']['precipType']))
 +
  
 **So, if the data structure response[‘currently’] has a key of ‘precipType’, we’ll print it, otherwise we’ll just go on without any problems. That’s the ‘key in dictionary’ part of the if clause. Again, to many of you, we’ve done this kind of thing before so it’s old hat. To others, this is new information. **So, if the data structure response[‘currently’] has a key of ‘precipType’, we’ll print it, otherwise we’ll just go on without any problems. That’s the ‘key in dictionary’ part of the if clause. Again, to many of you, we’ve done this kind of thing before so it’s old hat. To others, this is new information.
Ligne 193: Ligne 211:
  
 response = session.get(url, timeout=timeout).json()** response = session.get(url, timeout=timeout).json()**
 +
 +Aussi, si dans la structure de données de réponse[‘currently’] la clé « preciType » est présente, nous l'imprimerons ; autrement, nous sortirons sans problème. C'est la partie « clé dans le distionnaire » de la clause if. À nouveau, pour beaucoup d'entre vous, nous avons déjà fait de telles choses avant ; c'est donc un sujet usé. Pour les autres ce sont des informations nouvelles.
 +
 +Maintenant, regardons, la manière d'obtenir la données en direct. 
  
 **Now we have the JSON data structure in our dictionary, just like we read it from the local file earlier. So now, you can comment our four lines that read the local file, and add the lines above. This way, if you want to play some more, you can still work with the local file by commenting out the “live” request code and uncommenting the local file-read code. **Now we have the JSON data structure in our dictionary, just like we read it from the local file earlier. So now, you can comment our four lines that read the local file, and add the lines above. This way, if you want to play some more, you can still work with the local file by commenting out the “live” request code and uncommenting the local file-read code.
issue146/python.txt · Dernière modification : 2019/07/16 23:06 de andre_domenech