我有一个从phpmyadmin导出的json文件,如下所示(utf-8文件):
[{"user_email": "bh549@sina.cn","followee_id": 1411833182,"create_date": "cdatetime datetime p1 (S\'\\x07\\xdb\\x06\\x13\\x16\\x08(\\r\\xd5\\xcc\' tRp2 ."}, {"user_email": "zaici4@sina.cn","followee_id": 1296426000,"create_date": "cdatetime datetime p1 (S\'\\x07\\xdb\\x07\\x14\\x179\\x16\\x02 \\x08\' tRp2 ."}, {"user_email": "yanaa357@sina.com","followee_id": 1848085255,"create_date": "cdatetime datetime p1 (S\'\\x07\\xdb\\x08\\x13\\x17\\x10\\x0f\\x05\\x1c\\x02\' tRp2 ."}]
每个dict是数据库中的一行,每行中的第三个值是一个cpickled字符串。在
然后我使用表单将这个文件上传到python脚本(使用post方法)。在
然后在python脚本中解析该文件,如下所示:
^{pr2}$然后浏览器在执行时打印值错误json.loads公司名称:
<type 'exceptions.ValueError'>: Invalid control character at: line 1 column 83 (char 83)
第一行中的第三个char空格是83。在
如何解决这个问题?在
谢谢mhawke不管怎样。但是你说的第一个问题不存在。没有\n因为我是从打印结果复制的,在导出的json文件中,它实际上有\n
{"user_email": "bh549@sina.cn","followee_id": 1411833182,"create_date": "cdatetime
datetime
p1
(S\'\\x07\\xdb\\x06\\x13\\x16\\x08(\\r\\xd5\\xcc\'
tRp2
."}, {"user_email": "zaici4@sina.cn","followee_id": 1296426000,"create_date": "cdatetime
datetime
p1
(S\'\\x07\\xdb\\x07\\x14\\x179\\x16\\x02 \\x08\'
tRp2
."}
我是我误解了吗?第二个问题,它是phpmyadmin转义文件时导出的,那么怎么解决你说的问题?在
slouton:我编写了一个python脚本来导出表并转换pickled数据。它现在可以用pickled数据处理json。在
JSON不支持像
\x07
这样的十六进制转义。您必须使用unicode转义符(\u0007
)。在解码之前尝试将一种形式转换为另一种形式:有几个问题。在
首先是腌菜似乎是无效的(我认为)。不同组件之间缺少新行字符。e、 g
第二个问题是您需要用另一个
\
转义来转义\
。使用replace
可以很容易地做到这一点,例如replace('\\', '\\\\')
这段代码显示了包括pickled对象的往返JSON编码和解码:
^{pr2}$请注意
j
中的反斜杠是如何被另一个反斜杠转义的。这就是来自数据库的pickle应该是什么样子。对存储在j
中的JSON数据进行解码如下:在JSON的字符串中,换行符不是有效,如果phpMyAdmin正在生成它们,那么您应该在项目中记录一个bug。在
相关问题 更多 >
编程相关推荐