SpyderManGib
Posts: 4
Joined: Sun Jan 03, 2016 2:40 pm

hcm5883l split the data

Sun Jan 03, 2016 3:11 pm

Hi,

I have recently started using Python3 and need to extract the individual values from the command.
print(hcm5883l).
It displays something like
Bearing 232.12
Tilt 12.121
angle 3.5454

etc
How can I make those values variables so I can use them

Thanks
Albert

DirkS
Posts: 10583
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: hcm5883l split the data

Sun Jan 03, 2016 3:22 pm

Apparently you're using a module that you have downloaded from somehwere.
Do you have a link for that?

enedil
Posts: 80
Joined: Sat Feb 21, 2015 4:22 pm
Location: Toruń, Poland
Contact: Website

Re: hcm5883l split the data

Sun Jan 03, 2016 6:18 pm

If that's a string, you can use split:

Code: Select all

list(map(lambda p: float(p.split(' ')[1]), string.split('\n')))
- What Can a Thoughtful Man Hope for Mankind on Earth, Given the Experience of the Past Million Years?
- Nothing.

Kurt Vonnegut, Cat's Cradle

DirkS
Posts: 10583
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: hcm5883l split the data

Sun Jan 03, 2016 6:20 pm

enedil wrote:If that's a string, you can use split:

Code: Select all

list(map(lambda p: p.split(' ')[1], string.split('\n')))
You could, but if the OP uses a downloaded module, it's likely that there are functions to get the 'raw' information.

SpyderManGib
Posts: 4
Joined: Sun Jan 03, 2016 2:40 pm

Re: hcm5883l split the data

Sun Jan 03, 2016 9:58 pm

Hi,

I followed this to get ti working.

http://www.instructables.com/id/Interfa ... /?ALLSTEPS

The picture at the bottom shows the output im getting.

Any clear help would be great as im really really new to python.

Thanks

Albert

User avatar
DougieLawson
Posts: 40787
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: hcm5883l split the data

Sun Jan 03, 2016 10:49 pm

So the code library you're using is https://bitbucket.org/thinkbowl/i2clibraries.git

https://bitbucket.org/thinkbowl/i2clibr ... mc5883l.py is the module reading your sensor.
Any language using left-hand whitespace for syntax is ridiculous

Any DMs sent on Twitter will be answered next month.
Fake doctors - are all on my foes list.

Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

SpyderManGib
Posts: 4
Joined: Sun Jan 03, 2016 2:40 pm

Re: hcm5883l split the data

Sun Jan 03, 2016 11:10 pm

DougieLawson wrote:So the code library you're using is https://bitbucket.org/thinkbowl/i2clibraries.git

https://bitbucket.org/thinkbowl/i2clibr ... mc5883l.py is the module reading your sensor.
Im sorry but that means nothing to me.

Does it help someone else help me separate the info I need

Thanks
Albert

SpyderManGib
Posts: 4
Joined: Sun Jan 03, 2016 2:40 pm

Re: hcm5883l split the data

Sun Jan 03, 2016 11:50 pm

Although I can do something like

message2 = str(hcm5883l)

I can then not get the len(message2)

I can though use the message2[2:5]
which gives me an extract from that "string"

I have tried the spli command and it fails.

Any example I could use to split the message2 string wherever it has a carriage return and return
X,Y,Z,Beating

Would be great.

Keep in mind I have very little experience in python3 and used to basic but learing python.

Thanks
Albert

User avatar
DougieLawson
Posts: 40787
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: hcm5883l split the data

Sun Jan 03, 2016 11:58 pm

Try

Code: Select all

message2 = str(hcm5883l)
print dir(message2)
Any language using left-hand whitespace for syntax is ridiculous

Any DMs sent on Twitter will be answered next month.
Fake doctors - are all on my foes list.

Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

DirkS
Posts: 10583
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: hcm5883l split the data

Mon Jan 04, 2016 8:45 am

'prnt(hcm5883l)' uses this code:

Code: Select all

	def __str__(self):
		ret_str = ""
		(x, y, z) = self.getAxes()
		ret_str += "Axis X: "+str(x)+"\n"       
		ret_str += "Axis Y: "+str(y)+"\n" 
		ret_str += "Axis Z: "+str(z)+"\n" 
		
		ret_str += "Declination: "+self.getDeclinationString()+"\n" 
		
		ret_str += "Heading: "+self.getHeadingString()+"\n" 
		
		return ret_str
So the followings should be available:
* hcm5883l.getAxes()
* hcm5883l.getDeclinationString()
* hcm5883l.getHeadingString()
I can also see some variations that give a numrical output instead of a string:
* hcm5883l.getDeclination
* hcm5883l.getHeading

Return to “Python”