4

I get the data as type <type 'unicode'>

u'{0.128,0.128,0.133,0.137,0.141,0.146,0.15,0.155,0.159,0.164,0.169,0.174,0.179,0.185,0.19,0.196,0.202,0.208,0.214,0.22}'  

I want to convert this to list like

[0.128,0.128,0.133,0.137,0.141,0.146,0.15,0.155,0.159,0.164,0.169,0.174,0.179,0.185,0.19,0.196,0.202,0.208,0.214,0.22]

How can I do this with python?

Thank you

2 Answers 2

8

Just like that:

>>> a = u'{0.128,0.128,0.133,0.137,0.141,0.146,0.15,0.155,0.159,0.164,0.169,0.174,0.179,0.185,0.19,0.196,0.202,0.208,0.214,0.22}'
>>> [float(i) for i in a.strip('{}').split(',')]
[0.128, 0.128, 0.133, 0.137, 0.141, 0.146, 0.15, 0.155, 0.159, 0.164, 0.169, 0.174, 0.179, 0.185, 0.19, 0.196, 0.202, 0.208, 0.214, 0.22]

Unicode is very similar to str and you can use .split(), as well as strip(). Furthermore, casting to float works the way it works for str.

So, first strip your string of the unnecessary curly braces ({ and }) by using .strip('{}'), then split the resulting string by commas (,) using .split(','). After that you can just use list comprehension, converting each item to float, as in the example above.

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

Comments

4

Out of my head and untested:

data = u'your string with braces removed'
aslist = [float(x) for x in data.split(',')]

Comments

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.