Dictionary string parser

Sebastjan Trepca trepca at gmail.com
Tue Nov 22 12:56:54 EST 2005


Wow, this helps a LOT! It's just what I needed, thank you very much! :)

Sebastjan

On 22/11/05, Fredrik Lundh <fredrik at pythonware.com> wrote:
> Sebastjan Trepca wrote:
>
> > is there any library or some way to parse dictionary string with list,
> > string and int objects into a real Python dictionary?
> >
> > For example:
> >
> > >>> my_dict = dict_parser("{'test':'123','hehe':['hooray',1]}")
> >
> > I could use eval() but it's not very fast nor secure.
>
> it's definitely slower than eval (which is written in C, after all), but it's
> definitely more limited, and hopefully also more secure:
>
> import cStringIO
> import tokenize
>
> def _parse(token, src):
>     if token[1] == "{":
>         out = {}
>         token = src.next()
>         while token[1] != "}":
>             key = _parse(token, src)
>             token = src.next()
>             if token[1] != ":":
>                 raise SyntaxError("malformed dictionary")
>             value = _parse(src.next(), src)
>             out[key] = value
>             token = src.next()
>             if token[1] == ",":
>                 token = src.next()
>         return out
>     elif token[1] == "[":
>         out = []
>         token = src.next()
>         while token[1] != "]":
>             out.append(_parse(token, src))
>             token = src.next()
>             if token[1] == ",":
>                 token = src.next()
>         return out
>     elif token[0] == tokenize.STRING:
>         return token[1][1:-1].decode("string-escape")
>     elif token[0] == tokenize.NUMBER:
>         try:
>             return int(token[1], 0)
>         except ValueError:
>             return float(token[1])
>     else:
>         raise SyntaxError("malformed expression")
>
> def myeval(source):
>     src = cStringIO.StringIO(source).readline
>     src = tokenize.generate_tokens(src)
>     return _parse(src.next(), src)
>
> print myeval("{'test':'123','hehe':['hooray',0x10]}")
> {'test': '123', 'hehe': ['hooray', 16]}
>
> hope this helps!
>
> </F>
>
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list