473,396 Members | 1,712 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

string to integer problem

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
8 4304
> 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
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
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
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
> 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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: KRoy | last post by:
I have a password stored in the Registry encrypted using System.Security.Cryptography DES Algorithm. I supplied it a password and a Initialization Vector. I am trying to decrypt it using the...
11
by: Zordiac | last post by:
How do I dynamically populate a string array? I hope there is something obvious that I'm missing here Option Strict On dim s() as string dim sTmp as string = "test" dim i as integer ...
6
by: Eric | last post by:
.... my eternal gratitude!!! :p Here is the problem. A sample of my original VB6 code : '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Public...
20
by: MLH | last post by:
120 MyString = "How many copies of each letter do you need?" 150 MyVariant = InputBox(MyString, "How Many?", "3") If MyVariant = "2" Then MsgBox "MyVariant equals the string '2'" If...
5
by: ThatVBGuy | last post by:
Hello All, I could really use some help with this problem its driving me nuts. I have a small vb app, the goal of the app is to read an html doc into a variable then go through that variable and...
11
by: RipperT | last post by:
Don't know if this group covers web apps, but here goes. In VS 2005, I am trying to get variables to hold thier values during postback from the server. I convert a text box's user-keyed value to an...
12
by: Pascal | last post by:
hello and soory for my english here is the query :"how to split a string in a random way" I try my first shot in vb 2005 express and would like to split a number in several pieces in a random way...
1
by: Phoenix | last post by:
Hi Friends, Any help would be highly appreciated for the following problem : I have a vb 6 application which makes call to an API in some dll which returns an array of strings and no of...
11
by: xyz | last post by:
I have a string 16:23:18.659343 131.188.37.230.22 131.188.37.59.1398 tcp 168 for example lets say for the above string 16:23:18.659343 -- time 131.188.37.230 -- srcaddress 22 ...
2
by: =?Utf-8?B?QXNzaWRv?= | last post by:
Hello @all i need help with the following problem: Im calling an unmanaged C++ DLL (TAPI32.dll) function (lineGetCallInfo) like this: Declare Function lineGetCallInfo Lib "tapi32.dll"...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.