如果在JSON(Python)中出现逗号错误,我应该使用marshal吗?

2024-04-16 13:08:45 发布

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

实际上,我正在使用Python本身转储一个巨大的数据结构(多个列表和字典),并通过套接字将其发送给客户机。你知道吗

每次运行程序时,我都会在不同的位置得到一个ValueError: 'Expecting ',' delimiter: line 1 column 16177 (char 16176)(可能是列25000,也可能是列13000,它一直在变化)。你知道吗

我应该使用marshal而不是json(甚至pickle)吗?对于大文件大小,最可靠的格式是什么?你知道吗


Tags: 程序json数据结构列表客户机字典linecolumn
1条回答
网友
1楼 · 发布于 2024-04-16 13:08:45

我建议使用pickle(如果您使用的是python2.X,则使用cPickle),因为它几乎可以序列化任何内容,包括用户定义的类。正如医生所说

The marshal serialization format is not guaranteed to be portable across Python versions. Because its primary job in life is to support .pyc files, the Python implementers reserve the right to change the serialization format in non-backwards compatible ways should the need arise. The pickle serialization format is guaranteed to be backwards compatible across Python releases.

(我的)。你知道吗

pickle的另一个优点是:

The pickle module keeps track of the objects it has already serialized, so that later references to the same object won’t be serialized again. marshal doesn’t do this.

This has implications both for recursive objects and object sharing. Recursive objects are objects that contain references to themselves. These are not handled by marshal, and in fact, attempting to marshal recursive objects will crash your Python interpreter. Object sharing happens when there are multiple references to the same object in different places in the object hierarchy being serialized. pickle stores such objects only once, and ensures that all other references point to the master copy. Shared objects remain shared, which can be very important for mutable objects.

如果pickle无法序列化某些数据,也可以使用^{}。你知道吗

相关问题 更多 >