3

I have a string like this one:

l1="[{'t_steps': '', 't_expected': '', 't_type': 'Functional', 't_precond': 'xxx', 't_notes': 'test note', 't_name': 'First test'}]"

I need to transform it to a real list object like:

l1=[{'t_steps': '', 't_expected': '', 't_type': 'Functional', 't_precond': 'xxx', 't_notes': 'test note', 't_name': 'First test'}]

I tried:

l1=list(l1)

or

l1=l1.split(',')

But the result is not good. Please can someone help me to convert my string to a form that python can read?

So many thanks in advance

Jesse Reza Khorasanee
  • 3,140
  • 4
  • 36
  • 53
Manuel Santi
  • 1,106
  • 17
  • 46

3 Answers3

3

use eval

l1="[{'t_steps': '', 't_expected': '', 't_type': 'Functional', 't_precond': 'xxx', 't_notes': 'test note', 't_name': 'First test'}]"

do this:

l1 = eval(l1)
l1

output:

[{'t_steps': '', 't_expected': '', 't_type': 'Functional', 't_precond': 'xxx', 't_notes': 'test note', 't_name': 'First test'}]
xiutiqianshi
  • 209
  • 1
  • 10
  • Hi thanks a lot, the problem is when the string contain a null value like this: '[{"tk_kval": "yyy", "tk_kgroup": null, "tk_descr": "Create key", "t_owner": 1}]' – Manuel Santi Sep 11 '19 at 04:00
  • I don't quite understand what you mean,can u explain? – xiutiqianshi Sep 11 '19 at 04:03
  • if u run l1=eval('[{"tk_kval": "yyy", "tk_kgroup": null, "tk_descr": "Create key", "t_owner": 1}]') you got an error because null value – Manuel Santi Sep 11 '19 at 04:08
0

An alternative solution using json:

import json

l1="[{'t_steps': '', 't_expected': '', 't_type': 'Functional', 't_precond': 'xxx', 't_notes': 'test note', 't_name': 'First test'}]"
l2 = json.loads(l1)
s0mbre
  • 361
  • 2
  • 14
  • This won't work. The JSON should use ```"``` instead of ```'``` for this to work. You can use ```l1.replace("'","\"")``` but this will be a hack instead of an actual solution. – Amit Singh Sep 11 '19 at 03:54
  • Well, yeah, thanks for pointing out. Need to replace the single quotes for double ones first. To me it still looks more natural than evaluation via ast (which does seem a dirty hack). But tastes differ! – s0mbre Sep 11 '19 at 04:21
  • There is a built-in available ```eval``` that does the same job as pointed out by @xiutiqianshi – Amit Singh Sep 11 '19 at 04:30
0

Here in code you can parse.

x = "[{'t_steps': '', 't_expected': '', 't_type': 'Functional', 't_precond': 'xxx', 't_notes': 'test note', 't_name': 'First test'}]"
import re
# remove the [] and {} from the string
x = x.replace('[',"").replace("]","").replace("{","").replace("}","")
# split the string considering ',' and ite will return a list
x = x.split(",")
new_dict ={}
for values in x:
    # remove the "'" from the string
    values = re.sub("'*", '', values)
    values = values.replace('"', '')
    # split the key value pair from the list ':' and
    key, value = values.split(":")
    key = key.strip()
    value = value.strip()
    if value == "null":
        value = None
    else:
        # parsing the string value ton int or float
        try:
            if "." in value:
                value = float(value)
            else:
                value = int(value)
        except:
            pass
    new_dict[key.strip()] = value
final_list = []
final_list.append(new_dict)
print(final_list)
R.A.Munna
  • 1,699
  • 1
  • 15
  • 29
  • Hi thanks a lot, the problem is when the string contain a null value like this: '[{"tk_kval": "yyy", "tk_kgroup": null, "tk_descr": "Create key", "t_owner": 1}]' – Manuel Santi Sep 11 '19 at 04:01
  • @ManuelSanti I have updated the answer. Please check. Thank you. – R.A.Munna Sep 11 '19 at 04:43