Be Excellent To Each Other https://www.beexcellenttoeachother.com/forum/ |
|
Python help? https://www.beexcellenttoeachother.com/forum/viewtopic.php?f=3&t=7310 |
Page 1 of 1 |
Author: | kalmar [ Thu Jun 09, 2011 9:17 ] |
Post subject: | Python help? |
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? |
Author: | Doctor Glyndwr [ Thu Jun 09, 2011 10:02 ] |
Post subject: | Re: Python help? |
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.
|
Author: | Pod [ Thu Jun 09, 2011 10:06 ] |
Post subject: | Re: Python help? |
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. |
Author: | kalmar [ Thu Jun 09, 2011 10:18 ] |
Post subject: | Re: Python help? |
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. |
Author: | Doctor Glyndwr [ Thu Jun 09, 2011 10:22 ] |
Post subject: | Re: Python help? |
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. |
Author: | kalmar [ Thu Jun 09, 2011 10:42 ] |
Post subject: | Re: Python help? |
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. |
Author: | kalmar [ Thu Jun 09, 2011 11:11 ] |
Post subject: | Re: Python help? |
The answer is ORD() ! Thanks guys, you helped me understand the data type. |
Author: | Mimi [ Thu Jun 09, 2011 11:25 ] |
Post subject: | Re: Python help? |
Well, not being able to see the subtitle, this thread was a disappointment. |
Author: | Pod [ Thu Jun 09, 2011 12:00 ] |
Post subject: | Re: Python help? |
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: '`' |
Author: | lasermink [ Fri Jun 10, 2011 10:07 ] |
Post subject: | Re: Python help? |
I think Beex just crashed. |
Author: | Mr Russell [ Fri Jun 10, 2011 10:20 ] |
Post subject: | Re: Python help? |
lasermink wrote: I think Beex just crashed. Into your mum's face! |
Author: | Mimi [ Sat May 16, 2020 13:04 ] |
Post subject: | Re: Python help? |
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 —— |
Author: | myp [ Sat May 16, 2020 13:45 ] |
Post subject: | Re: Python help? |
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). |
Author: | Mimi [ Sat May 16, 2020 19:26 ] |
Post subject: | Re: Python help? |
Thank you. I passed that on x |
Page 1 of 1 | All times are UTC [ DST ] |
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group http://www.phpbb.com/ |