-2

I want to set a parameter of an .ini file. I use the configparser module, but I notice that every time I save a parameter, it is stored as a string. It there a possibility to convert a string formatted as a list, like "['p1', 'p2', 'p3']", into a list?

This is what I do:

list1 = ['ALL'] # initial list value

tmpList = cfg.get('DEFAULT', 'list1')
for i in range(0, len(tmpList)):
    list1.insert(tk.END, tmpList[i])

He returns, with a print:

[
'
A
L
L
'
]
martineau
  • 119,623
  • 25
  • 170
  • 301
nn65
  • 31
  • 1
  • 1
  • 7
  • Could you provide a complete example? It's not possible to reproduce your problem right away. How do you print your output? – normanius Nov 02 '19 at 14:57
  • With the print command directly into the shell. But I solved thanks to @beer44. – nn65 Nov 02 '19 at 17:50

1 Answers1

0

You can convert python valid string object to actaul by using of ast.literal_eval

import ast
l1 = "['p1', 'p2', 'p3']"
l2 = ast.literal_eval(l1)
>>> l2
>>> ['p1', 'p2', 'p3']
>>> type(l2)
>>> <class 'list'>
Saleem Ali
  • 1,363
  • 11
  • 21