Reputation: 65
Hi everyone I have a simple problem but I don't find the solution, I have a function that returns something like that
[[4, 'adr', 0, 0, 1, '2016-04-05T13:00:01'], [115, 'adr', 0, 0, 1, '2016-04-05T14:00:01'], [226, 'adr', 0, 0, 1, '2016-04-05T15:00:01'], [337, 'adr', 0, 0, 1, '2016-04-05T16:00:01']]
when I check de type of this variable type(data)
say that is a string <type 'str'>
I want to create a loop to get each element like this
item 1 [4, 'adr', 0, 0, 1, '2016-04-05T13:00:01']
item 2 [115, 'adr', 0, 0, 1, '2016-04-05T14:00:01']
I try to convert the string in a list, a tuple... but nothing work, any idea how to change the string to any type that I can do a loop and get the items
when I try to convert in a tuple or string I have this result
('[', '[', '4', ',', ' ', "'", 'a', 'd', 'r', "'", ',', ' ', '0', ',', ' ', '0', ',', ' ', '1', ',', ' ', "'", '2', '0', '1', '6', '-', '0', '4', '-', '0', '5', 'T', '1', '3', ':', '0', '0', ':', '0', '1', "'", ']', ',', ' ', '[', '1', '1', '5', ',', ' ', "'", 'a', 'd', 'r', "'", ',', ' ', '0', ',', ' ', '0', ',', ' ', '1', ',', ' ', "'", '2', '0', '1', '6', '-', '0', '4', '-', '0', '5', 'T', '1', '4', ':', '0', '0', ':', '0', '1', "'", ']', ',', ' ', '[', '2', '2', '6', ',', ' ', "'", 'a', 'd', 'r', "'", ',', ' ', '0', ',', ' ', '0', ',', ' ', '1', ',', ' ', "'", '2', '0', '1', '6', '-', '0', '4', '-', '0', '5', 'T', '1', '5', ':', '0', '0', ':', '0', '1', "'", ']', ',', ' ', '[', '3', '3', '7', ',', ' ', "'", 'a', 'd', 'r', "'", ',', ' ', '0', ',', ' ', '0', ',', ' ', '1', ',', ' ', "'", '2', '0', '1', '6', '-', '0', '4', '-', '0', '5', 'T', '1', '6', ':', '0', '0', ':', '0', '1', "'", ']', ']')
Upvotes: 0
Views: 1524
Reputation: 65
Hi everyone I finally resolve the problem like this
data = {}
data = {'names': []}
for item in project_name:
data['names'].append(item)
data.update({item: {}})
jobs_running = []
jobs_pending = []
for row in all_rows:
if (str(item) == row[1]):
parsed_t = dp.parse(str(row[5]))
t_in_seconds = parsed_t.strftime('%s')
jobs_running.append([ (t_in_seconds), (row[3]) ])
jobs_pending.append([ (t_in_seconds), (row[4]) ])
data[item].update({'jobs_running': jobs_running})
data[item].update({'jobs_pending': jobs_pending})
So my data structure is like this see image
Upvotes: 0
Reputation: 2000
You could use ast.literal_eval:
import ast
myteststr = "[[4, 'adr', 0, 0, 1, '2016-04-05T13:00:01'], [115, 'adr', 0, 0, 1, '2016-04-05T14:00:01'], [226, 'adr', 0, 0, 1, '2016-04-05T15:00:01'], [337, 'adr', 0, 0, 1, '2016-04-05T16:00:01']]"
pyobj = ast.literal_eval(myteststr)
print(pyobj)
Upvotes: 1
Reputation: 1981
You might consider to use literal_eval from ast module.
In [8]: from ast import literal_eval
In [9]: a = "[[4, 'adr', 0, 0, 1, '2016-04-05T13:00:01'], [115, 'adr', 0, 0, 1, '2016-04-05T14:00:01'], [226, 'adr', 0, 0, 1, '2016-04-05T15:00:01'], [337, 'adr', 0, 0, 1, '2016-04-05T16:00:01']]"
In [10]: type(a)
Out[10]: str
In [11]: b = literal_eval(a)
In [12]: type(b)
Out[12]: list
In [13]: b
Out[13]:
[[4, 'adr', 0, 0, 1, '2016-04-05T13:00:01'],
[115, 'adr', 0, 0, 1, '2016-04-05T14:00:01'],
[226, 'adr', 0, 0, 1, '2016-04-05T15:00:01'],
[337, 'adr', 0, 0, 1, '2016-04-05T16:00:01']]
Then you have a proper list and can easily iterate on it to get it's element.
Upvotes: 3
Reputation: 49803
The easiest, and most dangerous, solution would be to do
eval( data )
Dangerous because you have to trust there is nothing malicious in that data.
You could write a regex to verify that the string/data is properly formatted; not knowing what that format is, I can't help with that.
Upvotes: 2