Nested lists/Dictionary strings conversion

Posted on 2005-05-06
Last Modified: 2008-02-01

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 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....

Question by:atulkamat
    1 Comment
    LVL 15

    Accepted Solution

    Use eval.

    >>> 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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Suggested Solutions

    Title # Comments Views Activity
    malformed field in csv file 6 117
    python/mysql  table update problem 10 70
    Python tuples 2 99
    distributed computing in Python 4 90
    Here I am using Python IDLE(GUI) to write a simple program and save it, so that we can just execute it in future. Because when we write any program and exit from Python then program that we have written will be lost. So for not losing our program we…
    Strings in Python are the set of characters that, once defined, cannot be changed by any other method like replace. Even if we use the replace method it still does not modify the original string that we use, but just copies the string and then modif…
    Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
    Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now