473,320 Members | 1,914 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,320 software developers and data experts.

How to clean up string in to list of tuples of floats

Hello I want to ask for some help here, I got this data as a string

s = "[(0, 0.5, 0), (3.7, 5, 6), (5, 6, 7), (9, 9, 9), (12.607, 10, 2)]"

but I would like to have it as it were actual list of tuples with floats.

any tips would be great.
Thank you
May 18 '10 #1

✓ answered by Glenton

This kind of thing is one of the great strengths of python, and similar. The eval command is probably what you're after. Please note that this command needs to be used with caution - as spiderman's uncle said: "with great power, comes great responsibility!"

Anyway, here's an interactive session:
Expand|Select|Wrap|Line Numbers
  1. In [1]: s = "[(0, 0.5, 0), (3.7, 5, 6), (5, 6, 7), (9, 9, 9), (12.607, 10, 2)]"
  2. In [2]: s2=eval(s)
  3.  
  4. In [3]: s2
  5. Out[3]: 
  6. [(0, 0.5, 0),
  7.  (3.7000000000000002, 5, 6),
  8.  (5, 6, 7),
  9.  (9, 9, 9),
  10.  (12.606999999999999, 10, 2)]
  11.  

2 1475
Glenton
391 Expert 256MB
This kind of thing is one of the great strengths of python, and similar. The eval command is probably what you're after. Please note that this command needs to be used with caution - as spiderman's uncle said: "with great power, comes great responsibility!"

Anyway, here's an interactive session:
Expand|Select|Wrap|Line Numbers
  1. In [1]: s = "[(0, 0.5, 0), (3.7, 5, 6), (5, 6, 7), (9, 9, 9), (12.607, 10, 2)]"
  2. In [2]: s2=eval(s)
  3.  
  4. In [3]: s2
  5. Out[3]: 
  6. [(0, 0.5, 0),
  7.  (3.7000000000000002, 5, 6),
  8.  (5, 6, 7),
  9.  (9, 9, 9),
  10.  (12.606999999999999, 10, 2)]
  11.  
May 19 '10 #2
oh amazing : ) that's time saver i was starting to think I'll have to do all sort of striping, tokenizing , converting

thanks a lot Glenton, i'm go read up on eval more. so i know better
May 19 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: sigi | last post by:
i've got something like this in a headerfile: const char* const StringList= { "FirstString", "SecondString", .... } (haven't checked the syntax yet, but i guess it's ok?)
8
by: Madhusudan Singh | last post by:
Is it possible to convert a very long list of strings to a list of floats in a single statement ? I have tried float(x) and float(x) but neither work. I guess I would have to write a loop if...
0
by: R. Nachtsturm | last post by:
Hi, I would like to create a workable ExpandableObjectConverter for a String Collection (class inheriting from CollectionBase for example and only accepting/returning Strings via...
2
by: A.M | last post by:
Hi, Is there any online resource that gives examples about advanced python string, list and map operations? Today I saw this and I found that I have to work more on mentioned topics:
0
by: bruce | last post by:
hi... i have the following piece of code that i'm testing... it should be using/comparing two equal strings. apparently it doesn't. i've tried to do a "strip" to remove pre/post whitespace.. but...
13
by: psbasha | last post by:
Hi, How to remove the duplicate data from List,Tuples and Dictionary?. Thanks in advance PSB
12
by: python101 | last post by:
I have a string list items = Assume that I don't know the order (index) of these items. I would like to remove the second 'B' out of the list without sorting or changing the the order of...
2
by: Assimalyst | last post by:
Hi I have a Dictionary<string, List<string>>, which i have successfully filled. My problem is I need to create a filter expression using all possible permutations of its contents. i.e. the...
9
by: gs | last post by:
is there any built in function or dotnet framework(version 2) to merge a generic list of string into one string with each element delimited by specified delimiting string? or do I have to roll...
2
by: michelqa | last post by:
Hi, I'm trying to display a list of string in the string collection editor just like the "items" property of an ListBox in VS IDE.... I can find many examples on the net but I cant find any...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.