string to integer problem

L

Lol McBride

Hi all,
I'm currently having problems with data returned from a MySQL database in
as much as the data returned is of the form '(1,10,23,33,35,48)' i.e. a
list variable holding 6 integers returned as a string.
I can't seem to get around how to return the data to it's original format
i.e. the list with 6 integers - I have tried slicing the string and
converting the charachters individually but then I run into trouble when I
have integers over two digits wide as I can't combine the two separate
digits back into the original number.
I feel sure that someone has come across this before and I hope that my
request for help on this will be answered.
Thanks ,
Lol McBride
 
R

Rich Krauter

I can't seem to get around how to return the data to it's original
format
i.e. the list with 6 integers - I have tried slicing the string and
converting the charachters individually but then I run into trouble when I
have integers over two digits wide as I can't combine the two separate
digits back into the original number.

l = '(1,10,23,33,35,48)'
[int(x) for x in l[1:-1].split(',')]
[1, 10, 23, 33, 35, 48]

Probably not the best way, but it seems to work.

Rich
 
D

Diez B. Roggisch

I'm currently having problems with data returned from a MySQL database in
as much as the data returned is of the form '(1,10,23,33,35,48)' i.e. a
list variable holding 6 integers returned as a string.
(1,10,23,33,35,48)
 
P

Peter Otten

Lol said:
I'm currently having problems with data returned from a MySQL database in
as much as the data returned is of the form '(1,10,23,33,35,48)' i.e. a
list variable holding 6 integers returned as a string.
I can't seem to get around how to return the data to it's original format
i.e. the list with 6 integers - I have tried slicing the string and
s = '(1,10,23,33,35,48)'
[int(n) for n in s[1:-1].split(",")]
[1, 10, 23, 33, 35, 48]

Peter
 
L

Lol McBride

Hi all,
I'm currently having problems with data returned from a MySQL database in
as much as the data returned is of the form '(1,10,23,33,35,48)' i.e. a
list variable holding 6 integers returned as a string.
I can't seem to get around how to return the data to it's original format
i.e. the list with 6 integers - I have tried slicing the string and
converting the charachters individually but then I run into trouble when I
have integers over two digits wide as I can't combine the two separate
digits back into the original number.
I feel sure that someone has come across this before and I hope that my
request for help on this will be answered.
Thanks ,
Lol McBride
Thank you for your replies - your help is very much appreciated.
 
L

Lol McBride

(1,10,23,33,35,48)
Hi,
thanks for this it worked a treat but I don't understand how - even after
reading the docs for the eval() builtin function.Could you explain what's
going on (in fairly simple terms as well - I'm a programmer for fun not
cause it's my job)
Thank you,
Lol
 
D

Diez B. Roggisch

Hi,
thanks for this it worked a treat but I don't understand how - even after
reading the docs for the eval() builtin function.Could you explain what's
going on (in fairly simple terms as well - I'm a programmer for fun not
cause it's my job)
Thank you,
Lol

eval is a function that simply evaluates the expression you pass to it as
string - so

eval("10 + 20")

yields 30. You could think of eval beeing a call to something that starts
the python interpreter as separete process/program, passes its arg as the
script to execute, and returns the computed result - thats it.
 
G

Gerrit

Lol said:
Hi,
thanks for this it worked a treat but I don't understand how - even after
reading the docs for the eval() builtin function.Could you explain what's
going on (in fairly simple terms as well - I'm a programmer for fun not
cause it's my job)

Passing a string to 'eval' is equal in having it as a part of an
expression, for example as having in at the right side of an assignment
operator (=), except for the quotes. The string returned happens to be
valid Python syntax: numbers seperated by comma's with ( and ) around it
create a tuple. It is valid to simply onter (1,2,3) at the Python
interactive interpreter: this is why it is valid to pass '(1,2,3)' to
eval. The result is the same.

Hope this helps,
yours,
Gerrit.
 
A

Anton Vredegoor

Lol McBride said:
thanks for this it worked a treat but I don't understand how - even after
reading the docs for the eval() builtin function.Could you explain what's
going on (in fairly simple terms as well - I'm a programmer for fun not
cause it's my job)

Sorry, but programmers for fun are expected to do better than
microslaves! Resist this professional nonsense.

Anton
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,503
Latest member
supremedee

Latest Threads

Top