Reputation: 8291
I have the following text (one of the cells in pandas dataframe) that creates trouble when converting it to a json dictionary type:
{"options_selected":{"Ideas":"0"},"criterion_feedback":{},
"overall_feedback":"...and that\'s something I want to learn too. ",
"submission_uuid":"b195603a-60f5-11e4-95a7-0a7da95da37f"}
The code I am using is this:
df['POST'] = df['POST'].apply(yaml.load)
It throws the following error:
found unknown escape character "'"
in "<unicode string>", line 1, column 174:
... that\'s something I want to learn too ...
When I print that specific cell, this is what I got
df.ix[7, 'POST']
>> that\\\'s
I have checked already other relevant questions in SO and the YAML guide, but could not figure out what the solution is. Can someone help please?
Upvotes: 1
Views: 10900
Reputation: 5474
You will have to remove the escaped character first because JSON doesn't allow it.
import json
j = '{"options_selected":{"Ideas":"0"},"criterion_feedback":{}, "overall_feedback":"...and that\'s something I want to learn too. ", "submission_uuid":"b195603a-60f5-11e4-95a7-0a7da95da37f"}'
json.loads(j.replace("\\'", "'"))
Edit:
as @larks said in the comment,
that
'
doesn't need to be escaped (because the string is enclosed in double quotes)
and indeed your problem is hard to reproduce:
import yaml
import json
data = """
options_selected:
Ideas: '0'
criterion_feedback: {}
overall_feedback": ...and that\'s something I want to learn too. ,
submission_uuid : b195603a-60f5-11e4-95a7-0a7da95da37f
"""
print yaml.load(data)
j = '{"options_selected":{"Ideas":"0"},"criterion_feedback":{}, "overall_feedback":"...and that\'s something I want to learn too. ", "submission_uuid":"b195603a-60f5-11e4-95a7-0a7da95da37f"}'
print json.loads(j)
will both output:
{'overall_feedback"': "...and that's something I want to learn too. ,", 'submission_uuid': 'b195603a-60f5-11e4-95a7-0a7da95da37f', 'options_selected': {'Ideas': '0'}, 'criterion_feedback': {}}
Upvotes: 2