Bonjour à tous

Je suis novice avec Python (pika). Je suis désolé si ma question semble simple, mais je n'ai pas réussi à faire avec les exemples que j'ai trouvé

'ai un code Python que j'ai repris qui va recupérer des données qui son stocké sur un serveur rabbitMQ (MQTT).

Ca ca marhce bien.

la chaine de caratere se trouve dans 'body'
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
print(" [x] %r" % body)
    data = json.loads(body)
Voici le contenu de 'body'
{"position":{"value":0,"timestamp":"1483729658000","context":{"lat":46.21984,"lng":6.14123,"IMEI":"865067020466666","Battery":38,"Quality":"GSM","Tower":"228,03,1771,2d6f,45,57"}},"battery":38}
J'aimerais pouvoir affiché les valeur de lat ou lng ou Battery ou encore value

J'ai donc essyé de faire ceci
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
  data = json.loads(body)
    print('')
    print ("Battery: " + data["battery"])
    print ("Lat: " + data["lat"])
    print ("Lng:" +data["lng"])
    print('')
mais évidemment, ca part en qaquouette!!
Traceback (most recent call last):
File "receive_2.py", line 34, in <module>
channel.start_consuming()
File "/usr/lib/python2.7/dist-packages/pika/adapters/blocking_connection.py", line 917, in start_consuming
self.connection.process_data_events()
File "/usr/lib/python2.7/dist-packages/pika/adapters/blocking_connection.py", line 215, in process_data_events
if self._handle_read():
File "/usr/lib/python2.7/dist-packages/pika/adapters/blocking_connection.py", line 328, in _handle_read
super(BlockingConnection, self)._handle_read()
File "/usr/lib/python2.7/dist-packages/pika/adapters/base_connection.py", line 326, in _handle_read
self._on_data_available(data)
File "/usr/lib/python2.7/dist-packages/pika/connection.py", line 1271, in _on_data_available
self._process_frame(frame_value)
File "/usr/lib/python2.7/dist-packages/pika/connection.py", line 1351, in _process_frame
self._deliver_frame_to_channel(frame_value)
File "/usr/lib/python2.7/dist-packages/pika/connection.py", line 963, in _deliver_frame_to_channel
return self._channels[value.channel_number]._handle_content_frame(value)
File "/usr/lib/python2.7/dist-packages/pika/channel.py", line 791, in _handle_content_frame
self._on_deliver(*response)
File "/usr/lib/python2.7/dist-packages/pika/channel.py", line 886, in _on_deliver
body)
File "receive_2.py", line 25, in callback
print ("Battery " + data["battery"])
TypeError: cannot concatenate 'str' and 'int' objects
Je pense que je parse mal ma chaine de caractere en json et j'ai vraiment ausune idée comment le faire

Voici mon code complet
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
 
#!/usr/bin/env python
import pika
import json
 
connection = pika.BlockingConnection(pika.ConnectionParameters(
        host='localhost'))
channel = connection.channel()
 
channel.exchange_declare(exchange='log',
                         type='topic')
 
result = channel.queue_declare(exclusive=True)
#queue_name = result.method.queue
queue_name = 'fona'
print(queue_name)
channel.queue_bind(exchange='log',
                   queue=queue_name)
 
print('[*] Waiting for logs. To exit press CTRL+C')
 
def callback(ch, method, properties, body):
    print(" [x] %r" % body)
    data = json.loads(body)
    print('')
    print ("Battery " + data["battery"])
    print ("Lat " + data["lat"])
    print ("Lng " + data["lng"])
    print('')
 
channel.basic_consume(callback,
                      queue=queue_name,
                      no_ack=True)
 
channel.start_consuming()
Merci de me mettre sur la bonne piste!!!

PS: Ensuite, ces données récupérées, je veux les sauver dans une base myQSL...