Re: String to Dictionary conversion in python

2017-09-19 Thread sohcahtoa82
On Thursday, September 14, 2017 at 11:01:46 PM UTC-7, santosh.y...@gmail.com 
wrote:
> Hi,
> 
> Can anyone help me in the below issue.
> 
> I need to convert string to dictionary 
> 
> string = " 'msisdn': '7382432382', 'action': 'select', 'sessionId': '123', 
> 'recipient': '7382432382', 'language': 'english'"
> 
> Can anyone help me with the code

We're not going to do your homework for you.  What have you tried?  What errors 
are you getting?  If you're replying with your code or an error, make sure to 
use Copy/Paste and DO NOT just manually re-type the code or error.  Also, make 
sure to post the entire traceback.

If literal_eval is out of the question, take a look at the str.split() 
function.  I'd split on a comma, then for each result, split on the colon, then 
strip out the single quotes.

The actual coding of that is an exercise for the reader.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: String to Dictionary conversion in python

2017-09-16 Thread Piet van Oostrum
Rustom Mody  writes:

> On Saturday, September 16, 2017 at 2:04:39 AM UTC+5:30, jlad...@itu.edu wrote:

> Yeah… I used to think thus
> But literal_eval has excessive crud in its error messages:
>
 from ast import literal_eval
>
 literal_eval("{'x':1}")
> {'x': 1}
>
> Ok…
>
 literal_eval("{x:1}")
> Traceback (most recent call last):
>   File "", line 1, in 
>   File "/usr/lib/python2.7/ast.py", line 80, in literal_eval
> return _convert(node_or_string)
>   File "/usr/lib/python2.7/ast.py", line 63, in _convert
> in zip(node.keys, node.values))
>   File "/usr/lib/python2.7/ast.py", line 62, in 
> return dict((_convert(k), _convert(v)) for k, v
>   File "/usr/lib/python2.7/ast.py", line 79, in _convert
> raise ValueError('malformed string')
> ValueError: malformed string
>

You can catch the exception and print a nice error message:

from ast import literal_eval
def literal(s):
   try:
   return literal_eval('{'+s+'}')
   except Exception as e:
   print "%s: %s" % (type(e).__name__, ', '.join(e.args))

>>> literal("'x':1")
{'x': 1}
>>> literal("x:1")
ValueError: malformed string

But in non-interactive use you probably want to propagate the exception.

-- 
Piet van Oostrum 
WWW: http://piet.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: String to Dictionary conversion in python

2017-09-15 Thread Rustom Mody
On Saturday, September 16, 2017 at 2:04:39 AM UTC+5:30, jlad...@itu.edu wrote:
> On Thursday, September 14, 2017 at 11:33:56 PM UTC-7, Ian wrote:
> > On Fri, Sep 15, 2017 at 12:01 AM,   wrote:
> > > Hi,
> > >
> > > Can anyone help me in the below issue.
> > >
> > > I need to convert string to dictionary
> > >
> > > string = " 'msisdn': '7382432382', 'action': 'select', 'sessionId': 
> > > '123', 'recipient': '7382432382', 'language': 'english'"
> > >
> > > Can anyone help me with the code
> > 
> > It looks like this might do what you need:
> > 
> > py> import ast
> > py> string = " 'msisdn': '7382432382', 'action': 'select',
> > 'sessionId': '123', 'recipient': '7382432382', 'language': 'english'"
> > py> ast.literal_eval('{%s}' % string)
> > {'sessionId': '123', 'recipient': '7382432382', 'msisdn':
> > '7382432382', 'action': 'select', 'language': 'english'}
> 
> Very clever!  
Yeah… I used to think thus
But literal_eval has excessive crud in its error messages:

>>> from ast import literal_eval

>>> literal_eval("{'x':1}")
{'x': 1}

Ok…

>>> literal_eval("{x:1}")
Traceback (most recent call last):
  File "", line 1, in 
  File "/usr/lib/python2.7/ast.py", line 80, in literal_eval
return _convert(node_or_string)
  File "/usr/lib/python2.7/ast.py", line 63, in _convert
in zip(node.keys, node.values))
  File "/usr/lib/python2.7/ast.py", line 62, in 
return dict((_convert(k), _convert(v)) for k, v
  File "/usr/lib/python2.7/ast.py", line 79, in _convert
raise ValueError('malformed string')
ValueError: malformed string


>>> literal_eval("'x':1")
Traceback (most recent call last):
  File "", line 1, in 
  File "/usr/lib/python2.7/ast.py", line 49, in literal_eval
node_or_string = parse(node_or_string, mode='eval')
  File "/usr/lib/python2.7/ast.py", line 37, in parse
return compile(source, filename, mode, PyCF_ONLY_AST)
  File "", line 1
'x':1
   ^
SyntaxError: invalid syntax




> And definitely not an answer that would be acceptable for a homework 
> assignment.


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: String to Dictionary conversion in python

2017-09-15 Thread Ian Kelly
On Fri, Sep 15, 2017 at 12:01 AM,   wrote:
> Hi,
>
> Can anyone help me in the below issue.
>
> I need to convert string to dictionary
>
> string = " 'msisdn': '7382432382', 'action': 'select', 'sessionId': '123', 
> 'recipient': '7382432382', 'language': 'english'"
>
> Can anyone help me with the code

It looks like this might do what you need:

py> import ast
py> string = " 'msisdn': '7382432382', 'action': 'select',
'sessionId': '123', 'recipient': '7382432382', 'language': 'english'"
py> ast.literal_eval('{%s}' % string)
{'sessionId': '123', 'recipient': '7382432382', 'msisdn':
'7382432382', 'action': 'select', 'language': 'english'}
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: String to Dictionary conversion in python

2017-09-15 Thread Bill

santosh.yelamar...@gmail.com wrote:

Hi,

Can anyone help me in the below issue.

I need to convert string to dictionary

string = " 'msisdn': '7382432382', 'action': 'select', 'sessionId': '123', 
'recipient': '7382432382', 'language': 'english'"

Can anyone help me with the code


I'm new to Python too, but it looks like you need to "split" it about 
the commas. Right?

--
https://mail.python.org/mailman/listinfo/python-list