我有以下文本(pandas数据框中的一个单元格),在将其转换为json字典类型时会产生麻烦:
{"options_selected":{"Ideas":"0"},"criterion_feedback":{},
"overall_feedback":"...and that\'s something I want to learn too. ",
"submission_uuid":"b195603a-60f5-11e4-95a7-0a7da95da37f"}
我使用的代码是:
df['POST'] = df['POST'].apply(yaml.load)
它会抛出以下错误:
found unknown escape character "'"
in "<unicode string>", line 1, column 174:
... that\'s something I want to learn too ...
当我打印特定单元格时,这就是我得到的
df.ix[7, 'POST']
>> that\\\'s
我已经在SO和YAML指南中检查了其他相关问题,但无法弄清楚解决方案是什么。有人可以帮忙吗?
答案 0 :(得分:2)
您必须首先删除转义字符,因为JSON不允许它。
to_date
编辑:
正如@larks在评论中所说,
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("\\'", "'"))
不需要转义(因为该字符串包含在 double 引号中)
确实你的问题难以重现:
'
将输出:
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)