Posts: 52
Threads: 8
Joined: Apr 2017
Apr-20-2017, 11:23 PM
(This post was last modified: Apr-21-2017, 01:07 AM by ichabod801.)
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]
Posts: 4,229
Threads: 97
Joined: Sep 2016
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?
Posts: 52
Threads: 8
Joined: Apr 2017
Apr-21-2017, 01:26 PM
(This post was last modified: Apr-21-2017, 05:28 PM by Larz60+.)
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.
Posts: 7,092
Threads: 122
Joined: Sep 2016
Apr-21-2017, 02:09 PM
(This post was last modified: Apr-21-2017, 02:09 PM by snippsat.)
(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
Posts: 52
Threads: 8
Joined: Apr 2017
Posts: 52
Threads: 8
Joined: Apr 2017
Now trying to do the same for GPS (latitude and long). Also, how to relate the floating point values to the variables?
Posts: 7,092
Threads: 122
Joined: Sep 2016
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
Posts: 2,955
Threads: 48
Joined: Sep 2016
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
Posts: 52
Threads: 8
Joined: Apr 2017
Apr-24-2017, 10:34 PM
(This post was last modified: Apr-24-2017, 10:44 PM by kendias.)
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.
Posts: 7,092
Threads: 122
Joined: Sep 2016
(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'
|