Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1048
  • Last Modified:

Nested lists/Dictionary strings conversion

Hi,

i`ve got a nested list and a nested dictionary as a string...
i want to convert them into a nested list and a dictionary ..how do i go about doing that...
something like ['sdaf','asdf','sasfs',['sdfsd','asdfs','sadfs']]
and  ['sdfsd','sadfsd','sadfsaf,{'asdfas':'sdfasfsa','safasd':'asfsad',['sadfs','sadfasf']:['asdfsd','asdf']}]
I get the strings from a file..i`m trying to analyse some logs here and these things are printed in the logs...
Any python code wud be of great help....

Atul
0
atulkamat
Asked:
atulkamat
1 Solution
 
mish33Commented:
Use eval.

>>> eval("['sdaf','asdf','sasfs',['sdfsd','asdfs','sadfs']]")
eval("['sdaf','asdf','sasfs',['sdfsd','asdfs','sadfs']]")

>>> eval("['sdfsd','sadfsd','sadfsaf',{'asdfas':'sdfasfsa','safasd':'asfsad',['sadfs','sadfasf']:['asdfsd','asdf']}]")
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
  File "<string>", line 0, in ?
TypeError: list objects are unhashable
>>> 
You can't have a list as a dictionary key.

>>> eval("['sdfsd','sadfsd','sadfsaf',{'asdfas':'sdfasfsa','safasd':'asfsad',('sadfs','sadfasf'):['asdfsd','asdf']}]")
['sdfsd', 'sadfsd', 'sadfsaf', {'asdfas': 'sdfasfsa', ('sadfs', 'sadfasf'): ['asdfsd', 'asdf'], 'safasd': 'asfsad'}]

And catch SyntaxError as well, because use skipped closing quote in second example after sadfsaf.
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now