Be Excellent To Each Other

And, you know, party on. Dude.

All times are UTC [ DST ]




Reply to topic  [ 14 posts ] 
Author Message
 Post subject: Python help?
PostPosted: Thu Jun 09, 2011 9:17 
User avatar
baron of techno

Joined: 30th Mar, 2008
Posts: 24136
Location: fife
I'm attempting to program something in Python having never used it before, nor done any tutorials. Google gets you quite far but I've run into a problem and it's confusing me.

Right, so I have some bytes, in an array. Or possibly a string.

a=data[12]
print a

I get a character printed (Ý) which is obviously the ASCII character of the number I want.

I have tried using int() to convert it - and have used that elsewhere with success, but in this case I get an error - ValueError: invalid literal for int() with base 10: '\xed'

I think the problem is in understanding the type of data I've got. In C it would simply be an array of bytes, and extracting one like that gets you an integer, not a character.

?:|

Any clues?


Top
 Profile  
 
 Post subject: Re: Python help?
PostPosted: Thu Jun 09, 2011 10:02 
User avatar

Joined: 30th Mar, 2008
Posts: 32495
Quote:
I get a character printed (Ý) which is obviously the ASCII character of the number I want.
No, it isn't. Python's handling of numeric and string types is automatic. However you're populating data[], it doesn't contain what you think it contains.


Top
 Profile  
 
 Post subject: Re: Python help?
PostPosted: Thu Jun 09, 2011 10:06 
User avatar
MR EXCELLENT FACE

Joined: 30th Mar, 2008
Posts: 2568
type(a)

What does it say?


edit: Also, data[] is probably not an 'array', unless you explicilty made an array frmo some library like 'numpy'.
edit: and if it's a string, then data[4] would also be a string. (there's no such thing as a character).
So as Gaywood asks: How are you filling the data variable? More code plox.

_________________
This man is bound by law to clear the snow away


Top
 Profile  
 
 Post subject: Re: Python help?
PostPosted: Thu Jun 09, 2011 10:18 
User avatar
baron of techno

Joined: 30th Mar, 2008
Posts: 24136
Location: fife
It's stuff from the serial port - ser.read()
You're right, if I send ascii, I can print ascii.
It says <type 'str'>. OK.

In this case, I'm sending in numbers, like 0xED. I just need to get them out of the string, somehow.

Yeah, this is where I've got confused because in C a string and an array are interchangeable, so I wouldn't even be thinking about it.


Top
 Profile  
 
 Post subject: Re: Python help?
PostPosted: Thu Jun 09, 2011 10:22 
User avatar

Joined: 30th Mar, 2008
Posts: 32495
To give you an idea of how it's supposed to work

Code:
C:\Users\rgaywood\workspace_SSTS_2.6.1\FBECore>python
Python 2.7 (r27:82525, Jul  4 2010, 07:43:08) [MSC v.1500 64 bit (AMD64)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> data = [2,3,"4"]
>>> print data[0]
2
>>> print data[0]+data[1]
5
>>> print data[2]
4
>>> print data[2]+data[1]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects
>>> print int(data[2])+data[1]
7
>>> print data[2]+str(data[1])
43
>>>


You're not reading your serial port correctly.


Top
 Profile  
 
 Post subject: Re: Python help?
PostPosted: Thu Jun 09, 2011 10:42 
User avatar
baron of techno

Joined: 30th Mar, 2008
Posts: 24136
Location: fife
I am too! If I send 0x50 in the port, I get 'P'. Correct. That is also worth 80 in decimal, and I want that :)

VAL() in basic, possibly.. Back to google.


Top
 Profile  
 
 Post subject: Re: Python help?
PostPosted: Thu Jun 09, 2011 11:11 
User avatar
baron of techno

Joined: 30th Mar, 2008
Posts: 24136
Location: fife
The answer is ORD() !

Thanks guys, you helped me understand the data type.


Top
 Profile  
 
 Post subject: Re: Python help?
PostPosted: Thu Jun 09, 2011 11:25 
User avatar

Joined: 27th Mar, 2008
Posts: 21559
Well, not being able to see the subtitle, this thread was a disappointment.
:(

_________________
ImageImage


Top
 Profile  
 
 Post subject: Re: Python help?
PostPosted: Thu Jun 09, 2011 12:00 
User avatar
MR EXCELLENT FACE

Joined: 30th Mar, 2008
Posts: 2568
ps:

>>> data = "\x80"
>>> print data
Ç
>>> data
'\x80'
>>> repr(data)
"'\\x80'"

>>> data = "\x50\x36\x49"
>>> print data
P6I
>>> data
'P6I'
>>> repr(data)
"'P6I'"

>>> data + "\x80"
'P6I\x80'
>>> data = data + "\x80"
>>> print data
P6IÇ
>>> data
'P6I\x80'

>>> ord("\x60")
96
>>> int("\x60")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '`'

_________________
This man is bound by law to clear the snow away


Top
 Profile  
 
 Post subject: Re: Python help?
PostPosted: Fri Jun 10, 2011 10:07 
User avatar

Joined: 31st Mar, 2008
Posts: 1883
I think Beex just crashed.


Top
 Profile  
 
 Post subject: Re: Python help?
PostPosted: Fri Jun 10, 2011 10:20 
Awesome
User avatar
Yes

Joined: 6th Apr, 2008
Posts: 11827
lasermink wrote:
I think Beex just crashed.


Into your mum's face!

_________________
Always proof read carefully in case you any words out


Top
 Profile  
 
 Post subject: Re: Python help?
PostPosted: Sat May 16, 2020 13:04 
User avatar

Joined: 27th Mar, 2008
Posts: 21559
A friend has just sent me this asking if there is anyone I might know that might help:

An old work colleague of mine posted this on Facebook, any ideas?
—-

S.O.S my facebook pepes I need some HELP.
Any Abuntu or Python programmer could assist..
My son and I are build an autonomous rc car using donkeycar.
We keep PEMISSION DENIED after running the following command line:
python ~/mycar/manage.py train --tub ~/mycar/data/mytub --model ~/mycar/models/mypilot.h5

it error when writing back to the .h5 file

Please help as this is for his degree

Thank you all
——

_________________
ImageImage


Top
 Profile  
 
 Post subject: Re: Python help?
PostPosted: Sat May 16, 2020 13:45 
User avatar
Can't re-member

Joined: 27th Mar, 2008
Posts: 54482
Location: Liberty City
Sounds like either the file is set to read-only or the user account doesn’t have the correct privileges to write to the file, rather than a programming error as such (although I don’t do Python so I can’t tell if there’s any errors there).

_________________
I am currently under construction.
Thank you for your patience.


Image


Top
 Profile  
 
 Post subject: Re: Python help?
PostPosted: Sat May 16, 2020 19:26 
User avatar

Joined: 27th Mar, 2008
Posts: 21559
Thank you. I passed that on x

_________________
ImageImage


Top
 Profile  
 
Display posts from previous:  Sort by  
Reply to topic  [ 14 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Columbo, devilman, Vogons and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search within this thread:
You are using the 'Ted' forum. Bill doesn't really exist any more. Bogus!
Want to help out with the hosting / advertising costs? That's very nice of you.
Are you on a mobile phone? Try http://beex.co.uk/m/
RIP, Owen.

Powered by a very Grim... version of phpBB © 2000, 2002, 2005, 2007 phpBB Group.