By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,676 Members | 2,249 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,676 IT Pros & Developers. It's quick & easy.

string to integer problem

P: n/a
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

Jul 18 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
> 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.

s = '(1,10,23,33,35,48)'
t = eval(s)
print t

(1,10,23,33,35,48)

--
Regards,

Diez B. Roggisch
Jul 18 '05 #2

P: n/a
Lol McBride wrote:
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
Jul 18 '05 #3

P: n/a
On Sun, 08 Feb 2004 16:29:14 +0000, Lol McBride wrote:
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.
Jul 18 '05 #4

P: n/a
On Sun, 08 Feb 2004 18:00:11 +0100, Diez B. Roggisch wrote:
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.

s = '(1,10,23,33,35,48)'
t = eval(s)
print t

(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
Jul 18 '05 #5

P: n/a
> 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.

--
Regards,

Diez B. Roggisch
Jul 18 '05 #6

P: n/a
On Sun, 2004-02-08 at 11:29, Lol McBride wrote:
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

Jul 18 '05 #7

P: n/a
Lol McBride wrote:
On Sun, 08 Feb 2004 18:00:11 +0100, Diez B. Roggisch wrote:
> s = '(1,10,23,33,35,48)'
> t = eval(s)
> print t

(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)


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.

--
PrePEP: Builtin path type
http://people.nl.linux.org/~gerrit/c.../pep-xxxx.html
Asperger's Syndrome - a personal approach:
http://people.nl.linux.org/~gerrit/english/

Jul 18 '05 #8

P: n/a
Lol McBride <ne******@lolmc.com> wrote:
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
Jul 18 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.