1

I'm sure this is going to turn out to be a stupid question... I am trying to break up a string like s = 'P1=12,P2=34,P3=56,P4=78' into a bunch of individual variables:

P1 = 12
P2 = 34
P3 = 56
P4 = 78

s.split(',') gives me a list ['P1=12','P2=34','P3=56','P4=78'], which is a start, I think. Any ideas?

2
  • You want to turn these into actual executable Python statements? Commented Jul 18, 2012 at 10:55
  • The string will have been spat out by a scientific instrument - in this case the four Ps are the readings on four pressure sensors. I'd like to be able to store the readings in variables which can be frequently updated, and queried by the user. Commented Jul 18, 2012 at 10:56

3 Answers 3

8

I'd go with something like this:

d = {}
for assignment in s.split(","):
    name, value = assignment.split("=")
    d[name.strip()] = float(value)

This will give you a dictionary mapping the names to the values, which is most probably better than trying to create variable dynamically. I f you really want to do the latter, you could also do

exec s.replace(",", "\n")

but this would be really REALLY horrible.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for this, a dictionary would certainly do the job. Is there no neat way of breaking a dict up into individual floats?
@poorsod: Are the names of the variables fixed? Is it alyways the P1, P2, P3. P4?
I'd like to be able to reuse the code for different outputs. For example the same instrument, on a different command, might spit out a string like CRYO=0.1,1K=0.2,STILL=0.3,MC=0.4. I'd like to be able to send all sorts of strings like this to the same function which will spit out appropriately-named variables.
@poorsod: And with this string, you'd like to have a variable called 1K? Seems like the dictionary is the way to go.
|
4

Just double-split:

string = 'P1=12,P2=34,P3=56,P4=78'
d = dict( s.split('=') for s in string.split(',') )
# d == {'P2': '34', 'P3': '56', 'P1': '12', 'P4': '78'}

I've put these into a dict, as it may be handier for lookups depending on what you're using the data for.

If you wanted the value as an integer, you could do:

d = dict( (k, int(v)) for k, v in (s.split('=') for s in string.split(',')) )

Comments

0

Try this:

s = 'A=1,B=2,C=3'
d = dict([i.split('=') for i in s.split(',')])
o = '%s = %s' % (', '.join(d.keys()), ', '.join(d.values()))
exec(o)

print A, B, C
# 1, 2, 3

3 Comments

And I thought my solution using exec was horrible…
IMHO exec is horrible if you don't know the values its executing, here we're pretty much sure its just unpacking. :)
You have a point, given that this information comes from some instrument, exec might not be as bad as I thought in this case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.