0

I downloaded a csv table from a database using SQL. One of the fields has values like so:

'[0.1234,0.0,0.0]'

I want to convert this string to a python list to get the first value. I only know how to convert strings to ints and floats... is there any way to de-string this object? The table I got from SQL is from a web-based viewer, I'm not getting it from my command line.

0

2 Answers 2

1

You could take the substring from index 1 to index -1 and then split it using the comma as a delimiter. In python

array = variable[1:-1].split(',')

should work.

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

Comments

0

If you're sure it is always valid list syntax, you could use"

myList = eval('[0.1234,0.0,0.0]')

Or if the value itself has quotes ' in it, you can slice those off

value = "'[0.1234,0.0,0.0]'"
myList = eval(value[1:-1])

Then to get the first value you just

myList[0]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.