On Fri, Oct 15, 2010 at 6:26 AM, David Hutto <smokefl...@gmail.com> wrote:
> Ok, Let me restate and hopefully further clarify. > > 1. I have a field for a wxpython app using matplotlib to display > > 2. I have a sqlite3 db which I'm retrieving information from > > 3. The sqlitle data is returned as unicode: u'field' > > 4. The portion of the matplotlib code is filled in, in a for x in y: > > 5. in plot(self.plot), self.plot is the variable I'm using from the > unicoded db > field comes in from sqlite as u'[1,2,3,4]', which places a string in quotes > in > that variables place: > > plot(u'[1,2,3,4]') > > 6. the plot(eval(self.plot)), changes the variable from the u'[1,2,3,4]' > to just [1,2,3,4] > > 7 As stated somewhere above, the float error has nothing to do with > the probel, only the fact that it was used as if I had placed '' > around the necessary data from the db field. > > 8. If anyone has a way better than eval to convert the u'field' when > replacing a variable so that > > self.plot = [1,2,3,4] > > instead of > > self.plot = u'[1,2,3,4]' > > > Let me know, meanwhile I'll be reviewing the replies more thoroughly, > now that I've had a nap. > > > Thanks, > David > This will do it > So you want convert string u'1,2,3,4' to a list of ints [1,2,3,4]? > Then the below will work. > > [int(n) for n in u'1,2,3,4'.replace(',', '')] > > Greets > Sander > or this: >>> s = '1, 200 , -3,4' # or whatever >>> [int(x) for x in s.split(',')] [1, 200, -3, 4] or this: To take a string of comma separated integers and convert to a list of ints: >>> x = u'1,2,3,4' >>> y = x.split(',') >>> z = [int(f) for f in y] >>> z [1.0, 2.0, 3.0, 4.0] >>> -- Joel Goldstick You can forget about the u' in front. Its a directive to the interpreter that the following string is in unicode. In your case u'1,2,3,4' is the same as '1,2,3,4'. All of the above examples do this: 1. split the string up into 4 different strings in a tuple by removing the commas :: ('1', '2', '3', '4') 2. convert the individual string vales to integers and put them in a list :: [1, 2, 3, 4] -- Steven D'Aprano Joel Goldstick
_______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor