python如何解析phpmyadmin导出的json文件

2024-09-27 00:15:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个从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。在


Tags: 文件iddatetimedateemailcreatep1user
3条回答

JSON不支持像\x07这样的十六进制转义。您必须使用unicode转义符(\u0007)。在解码之前尝试将一种形式转换为另一种形式:

content = content.replace('\\x', '\\u00')

有几个问题。在

首先是腌菜似乎是无效的(我认为)。不同组件之间缺少新行字符。e、 g

import cPickle
bad_pickle = "cdatetime datetime p1 (S\'\\x07\\xdb\\x06\\x13\\x16\\x08(\\r\\xd5\\xcc\' tRp2 ."
good_pickle = '\n'.join(bad_pickle.split())

>>> cPickle.loads(bad_pickle)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
cPickle.UnpicklingError: pickle data was truncated

>>> cPickle.loads(good_pickle)
datetime.datetime(2011, 6, 19, 22, 8, 40, 906700)

第二个问题是您需要用另一个\转义来转义\。使用replace可以很容易地做到这一点,例如replace('\\', '\\\\')

这段代码显示了包括pickled对象的往返JSON编码和解码:

^{pr2}$

请注意j中的反斜杠是如何被另一个反斜杠转义的。这就是来自数据库的pickle应该是什么样子。对存储在j中的JSON数据进行解码如下:

d2 = json.loads(j)
cPickle.loads(d2['create_date'])

>>> d2
{'followee_id': 1411833182, 'create_date': "cdatetime\ndatetime\np1\n(S'\\x07\\xdc\\x06\\r\\x0c\\x16:\\x06/\\x85'\ntRp2\n.", 'user_email': 'bh549@sina.cn'}
>>> cPickle.loads(d2['create_date'])
datetime.datetime(2012, 6, 13, 12, 22, 58, 405381)

在JSON的字符串中,换行符不是有效,如果phpMyAdmin正在生成它们,那么您应该在项目中记录一个bug。在

>>> json.loads('"123"')
u'123'
>>> json.loads('"123\n456"')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python2.7/json/__init__.py", line 326, in loads
    return _default_decoder.decode(s)
  File "/usr/lib64/python2.7/json/decoder.py", line 366, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib64/python2.7/json/decoder.py", line 382, in raw_decode
    obj, end = self.scan_once(s, idx)
ValueError: Invalid control character at: line 1 column 4 (char 4)

相关问题 更多 >

    热门问题