Convert a string to a list in Python

I use as the framework of a website project. Recently I developed an iPhone app as the client of the website. The app use POST method to send accelerometer data to the website, and when the receive the data:

class sensor:
    def POST(self):
    data =
    print data

And the data is like this:


However, when I thought it was a list and try to get data[i], I found that it was

data[0] = '['
data[1] = '['
data[2] = '"'

which means, it is a string, not a list.

I searched online for a solution to convert a string to a list, someone suggested we use string = string[1:-1].split(',') or something. It was a lot of dirty work for my example. Finally, I found an elegant solution for this problem:

list_data = eval(data)

Just as simple as this! Now I can type

for entries in list_data:
    print entries

and get


Of course, the entries itself now is a list, too! 😀

Tagged , ,

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: