user254665 user254665 - 2 months ago 7
Python Question

convert string to other type in python

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', "'", ']', ']')

Answer

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