Python help?
The programming language.
Reply
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?
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.
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.
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.
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.
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.
The answer is ORD() !

Thanks guys, you helped me understand the data type.
Well, not being able to see the subtitle, this thread was a disappointment.
:(
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: '`'
I think Beex just crashed.
lasermink wrote:
I think Beex just crashed.


Into your mum's face!
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
——
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).
Thank you. I passed that on x
Page 1 of 1 [ 14 posts ]