Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
xml to integer
#1
my question here:

I am trying the foll. python script to get sensor values from my smart phone so as to do calcs on these values. Hence, I need to convert from xml format to integer or float. Not sure how.
import socket, traceback, string
from sys import stderr

host = ''
port = 5555
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
s.bind((host, port))

# print one blank line
print 

while 1:
   try:
       message, address = s.recvfrom(8192)
#        print message

# split records using comma as delimiter (data are streamed in CSV format)
       data = message.split( "," )

# convert to flaot for plotting purposes
       t = data[0]
       sensorID = int(data[1])
       if sensorID==3:     # sensor ID for the eccelerometer
           ax, ay, az = data[2], data[3], data[4]
Reply
#2
The point of the "my code here" thing was for you to put your code there. I fixed that for you this time, please make note of it for the future. More information can be found in the BBCode tutorial in my signature.

As to your question, I am confused. You say the data is in XML format, but the comment in your code says the records come in as CSV format. Can you be more specific about where the XML is coming into your code, and what it looks like?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Thanks. I ran it with the app from Google play store "Sensor Node Free" and get this:


Output:
# Example of XML data received: # <Node Id>node12</Node Id> # <GPS> # <Latitude>1.123123</Latitude> # <Longitude>234.1231231</Longitude> # <Accuracy>40.0</Accuracy> # </GPS> # <Accelerometer> # <Accelerometer1>0.38444442222</Accelerometer1> # <Accelerometer2>0.03799999939</Accelerometer2> # <Accelerometer3>9.19400000331</Accelerometer3> # </Accelerometer> # <TimeStamp>1370354489083</TimeStamp>
My question is - I want to use these readings to control a robot, do I have to convert this into another format and do the calcs or can I use as is.
Reply
#4
(Apr-21-2017, 01:26 PM)kendias Wrote: My question is - I want to use these readings to control a robot, do I have to convert this into another format and do the calcs or can I use as is.
Use a parer for XML data eg BeautifulSoup.
Here a demo,i insert regex to find all() to get all numbered Accelerometer1 2 3
Then loop over and convert to float()
from bs4 import BeautifulSoup
import re

xml ='''\
<Node Id>node12</Node Id>
<GPS>
  <Latitude>1.123123</Latitude>
  <Longitude>234.1231231</Longitude>
  <Accuracy>40.0</Accuracy>
</GPS>
<Accelerometer>
  <Accelerometer1>0.38444442222</Accelerometer1>
  <Accelerometer2>0.03799999939</Accelerometer2>
  <Accelerometer3>9.19400000331</Accelerometer3>
</Accelerometer>
<TimeStamp>1370354489083</TimeStamp>'''

soup = BeautifulSoup(xml, 'html.parser')
acc_meter = soup.find_all(re.compile("meter\d"))
for value in acc_meter:
    print(float(value.text))
Output:
0.38444442222 0.03799999939 9.19400000331
Reply
#5
Thanks a lot! Works fine
Reply
#6
Now trying to do the same for GPS (latitude and long). Also, how to relate the floating point values to the variables?
Reply
#7
You most look at doc,i also have tutorial here.
>>> soup = BeautifulSoup(xml, 'html.parser')
>>> lat = soup.find('latitude')
>>> lat
<latitude>1.123123</latitude>
>>> print(float(lat.text))
1.123123
Reply
#8
In [1]: from bs4 import BeautifulSoup

In [2]: with open('/tmp/test.xml', 'r') as xml_file:
    ...:     xml_data = xml_file.read()
    ...:     

In [3]: soup = BeautifulSoup(xml_data, 'lxml')

In [4]: print(soup.gps.latitude.text)

1.123123
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#9
Thanks Snipssat. Had a look at the tutorial but this part did not work (get error about syntax in HTML):
html = '''\
insert html
'''

<html> # Get error here
<head>
<title>My Site</title>
</head>
<body>
<title>First chapter</title>
<p>Page1</p>
<p>Page2</p>
</body>
</html>
Python Code: (Double-click to select all)
1
2
3
4
5
6
7
from bs4 import BeautifulSoup

soup = BeautifulSoup(html, 'html.parser')
print(soup.find('title'))
print(soup.find_all('title'))
#--> <title>My Site</title>
#--> [<title>My Site</title>, <title>First chapter</title>]

Thanks Wavic. Tried it but get error - forgive my ignorance about this.
Reply
#10
(Apr-24-2017, 10:34 PM)kendias Wrote: Thanks Snipssat. Had a look at the tutorial but this part did not work (get error about syntax in HTML)
Use code tag.
Use like this.
from bs4 import BeautifulSoup

html = '''\
<html>
  <head>
    <title>My Site</title>
  </head>
  <body>
    <title>First chapter</title>
    <p>Page1</p>
    <p>Page2</p>
  </body>
</html>'''

soup = BeautifulSoup(html, 'html.parser')
print(soup.find('title'))
print(soup.find_all('title'))
Output:
<title>My Site</title> [<title>My Site</title>, <title>First chapter</title>]
text to get contend.
>>> soup.find('title').text
u'My Site'
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020