Converting a string to dictionary in Python Value Error
up vote
0
down vote
favorite
I have tried different options to convert a string to dictionary.
My string looks like this:
'severity_label': 'Major', 'ne_reported_time': 1475424546, 'node_id': 54357, 'prob_cause_string': None
When i use
a_dict = dict([x.strip('').split(":"),])
it gives me an error:
Traceback (most recent call last):
File "<pyshell#168>", line 1, in <module>
a_dict = dict([x.strip('').split(":"),])
ValueError: dictionary update sequence element #0 has length 121; 2 is required
I am running this on Python3. Also tried various other options things not working. Any help appreciated.
python python-3.x
add a comment |
up vote
0
down vote
favorite
I have tried different options to convert a string to dictionary.
My string looks like this:
'severity_label': 'Major', 'ne_reported_time': 1475424546, 'node_id': 54357, 'prob_cause_string': None
When i use
a_dict = dict([x.strip('').split(":"),])
it gives me an error:
Traceback (most recent call last):
File "<pyshell#168>", line 1, in <module>
a_dict = dict([x.strip('').split(":"),])
ValueError: dictionary update sequence element #0 has length 121; 2 is required
I am running this on Python3. Also tried various other options things not working. Any help appreciated.
python python-3.x
1
You got the right answer (usingast.literal_eval()
) but you should really consider why you have a Python dictionary string representation in the first place. That is not a sane format to pass around information. Better use JSON or any other standard format. Or at least pickled data, as that is meant to be a serialization format.
– BlackJack
Feb 4 '16 at 15:20
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have tried different options to convert a string to dictionary.
My string looks like this:
'severity_label': 'Major', 'ne_reported_time': 1475424546, 'node_id': 54357, 'prob_cause_string': None
When i use
a_dict = dict([x.strip('').split(":"),])
it gives me an error:
Traceback (most recent call last):
File "<pyshell#168>", line 1, in <module>
a_dict = dict([x.strip('').split(":"),])
ValueError: dictionary update sequence element #0 has length 121; 2 is required
I am running this on Python3. Also tried various other options things not working. Any help appreciated.
python python-3.x
I have tried different options to convert a string to dictionary.
My string looks like this:
'severity_label': 'Major', 'ne_reported_time': 1475424546, 'node_id': 54357, 'prob_cause_string': None
When i use
a_dict = dict([x.strip('').split(":"),])
it gives me an error:
Traceback (most recent call last):
File "<pyshell#168>", line 1, in <module>
a_dict = dict([x.strip('').split(":"),])
ValueError: dictionary update sequence element #0 has length 121; 2 is required
I am running this on Python3. Also tried various other options things not working. Any help appreciated.
python python-3.x
python python-3.x
edited Feb 4 '16 at 12:18
JBernardo
22.3k56095
22.3k56095
asked Feb 4 '16 at 11:25
Praveeda
12
12
1
You got the right answer (usingast.literal_eval()
) but you should really consider why you have a Python dictionary string representation in the first place. That is not a sane format to pass around information. Better use JSON or any other standard format. Or at least pickled data, as that is meant to be a serialization format.
– BlackJack
Feb 4 '16 at 15:20
add a comment |
1
You got the right answer (usingast.literal_eval()
) but you should really consider why you have a Python dictionary string representation in the first place. That is not a sane format to pass around information. Better use JSON or any other standard format. Or at least pickled data, as that is meant to be a serialization format.
– BlackJack
Feb 4 '16 at 15:20
1
1
You got the right answer (using
ast.literal_eval()
) but you should really consider why you have a Python dictionary string representation in the first place. That is not a sane format to pass around information. Better use JSON or any other standard format. Or at least pickled data, as that is meant to be a serialization format.– BlackJack
Feb 4 '16 at 15:20
You got the right answer (using
ast.literal_eval()
) but you should really consider why you have a Python dictionary string representation in the first place. That is not a sane format to pass around information. Better use JSON or any other standard format. Or at least pickled data, as that is meant to be a serialization format.– BlackJack
Feb 4 '16 at 15:20
add a comment |
4 Answers
4
active
oldest
votes
up vote
1
down vote
Actually this is not JSON. This is a representation of a python object (like using the repr
function).
The most safe way to convert this back to a python object is to use the ast.literal_eval
function.
add a comment |
up vote
0
down vote
You should use json.loads
import json
a = '"severity_label": "Major", "ne_reported_time": 1475424546, "node_id": 54357, "prob_cause_string": null'
d = json.loads(a)
Note I replaced ' by " and None by null
add a comment |
up vote
0
down vote
Why not to use Python 3's inbuilt ast library's function literal_eval. It is better to use literal_eval instead of eval
import ast
str_of_dict = "'key1': 'key1value', 'key2': 'key2value'"
ast.literal_eval(str_of_dict)
will give output as actual Dictionary
'key1': 'key1value', 'key2': 'key2value'
This is easy as you like.
add a comment |
up vote
-1
down vote
That string really is valid JSON, I think. Just use the json.loads
functionality to get a dictionary.
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
Actually this is not JSON. This is a representation of a python object (like using the repr
function).
The most safe way to convert this back to a python object is to use the ast.literal_eval
function.
add a comment |
up vote
1
down vote
Actually this is not JSON. This is a representation of a python object (like using the repr
function).
The most safe way to convert this back to a python object is to use the ast.literal_eval
function.
add a comment |
up vote
1
down vote
up vote
1
down vote
Actually this is not JSON. This is a representation of a python object (like using the repr
function).
The most safe way to convert this back to a python object is to use the ast.literal_eval
function.
Actually this is not JSON. This is a representation of a python object (like using the repr
function).
The most safe way to convert this back to a python object is to use the ast.literal_eval
function.
answered Feb 4 '16 at 12:16
JBernardo
22.3k56095
22.3k56095
add a comment |
add a comment |
up vote
0
down vote
You should use json.loads
import json
a = '"severity_label": "Major", "ne_reported_time": 1475424546, "node_id": 54357, "prob_cause_string": null'
d = json.loads(a)
Note I replaced ' by " and None by null
add a comment |
up vote
0
down vote
You should use json.loads
import json
a = '"severity_label": "Major", "ne_reported_time": 1475424546, "node_id": 54357, "prob_cause_string": null'
d = json.loads(a)
Note I replaced ' by " and None by null
add a comment |
up vote
0
down vote
up vote
0
down vote
You should use json.loads
import json
a = '"severity_label": "Major", "ne_reported_time": 1475424546, "node_id": 54357, "prob_cause_string": null'
d = json.loads(a)
Note I replaced ' by " and None by null
You should use json.loads
import json
a = '"severity_label": "Major", "ne_reported_time": 1475424546, "node_id": 54357, "prob_cause_string": null'
d = json.loads(a)
Note I replaced ' by " and None by null
edited Feb 4 '16 at 11:29
Marcus Müller
23.2k32267
23.2k32267
answered Feb 4 '16 at 11:29
Benjamin
1,215727
1,215727
add a comment |
add a comment |
up vote
0
down vote
Why not to use Python 3's inbuilt ast library's function literal_eval. It is better to use literal_eval instead of eval
import ast
str_of_dict = "'key1': 'key1value', 'key2': 'key2value'"
ast.literal_eval(str_of_dict)
will give output as actual Dictionary
'key1': 'key1value', 'key2': 'key2value'
This is easy as you like.
add a comment |
up vote
0
down vote
Why not to use Python 3's inbuilt ast library's function literal_eval. It is better to use literal_eval instead of eval
import ast
str_of_dict = "'key1': 'key1value', 'key2': 'key2value'"
ast.literal_eval(str_of_dict)
will give output as actual Dictionary
'key1': 'key1value', 'key2': 'key2value'
This is easy as you like.
add a comment |
up vote
0
down vote
up vote
0
down vote
Why not to use Python 3's inbuilt ast library's function literal_eval. It is better to use literal_eval instead of eval
import ast
str_of_dict = "'key1': 'key1value', 'key2': 'key2value'"
ast.literal_eval(str_of_dict)
will give output as actual Dictionary
'key1': 'key1value', 'key2': 'key2value'
This is easy as you like.
Why not to use Python 3's inbuilt ast library's function literal_eval. It is better to use literal_eval instead of eval
import ast
str_of_dict = "'key1': 'key1value', 'key2': 'key2value'"
ast.literal_eval(str_of_dict)
will give output as actual Dictionary
'key1': 'key1value', 'key2': 'key2value'
This is easy as you like.
answered Nov 11 at 15:16
FightWithCode
295
295
add a comment |
add a comment |
up vote
-1
down vote
That string really is valid JSON, I think. Just use the json.loads
functionality to get a dictionary.
add a comment |
up vote
-1
down vote
That string really is valid JSON, I think. Just use the json.loads
functionality to get a dictionary.
add a comment |
up vote
-1
down vote
up vote
-1
down vote
That string really is valid JSON, I think. Just use the json.loads
functionality to get a dictionary.
That string really is valid JSON, I think. Just use the json.loads
functionality to get a dictionary.
answered Feb 4 '16 at 11:29
Marcus Müller
23.2k32267
23.2k32267
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f35199735%2fconverting-a-string-to-dictionary-in-python-value-error%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
You got the right answer (using
ast.literal_eval()
) but you should really consider why you have a Python dictionary string representation in the first place. That is not a sane format to pass around information. Better use JSON or any other standard format. Or at least pickled data, as that is meant to be a serialization format.– BlackJack
Feb 4 '16 at 15:20