简体   繁体   English

编写pickle.dump输出到文件

[英]Writing a pickle.dumps output to a file

I have the following code: 我有以下代码:

some_dict = {'a':0, 'b':1}
line = "some_dict_b = %s\n" % pickle.dumps(some_dict,2)
exec(line)
decoded_dict = pickle.loads(some_dict_b)
decoded_dict == some_dict

In python 3 this code prints True. 在python 3中,此代码显示True。 In python 2 (2.7.8) I get an error in the exec line. 在python 2(2.7.8)中,我在exec行中遇到错误。 I know dumps returns str in 2.7 while it returns a byte-stream in 3. 我知道转储在2.7中返回str,而在3中返回字节流。

I am writing a program that parses data from an input file then creates certain memory objects and should write out a python script that uses these objects. 我正在编写一个程序,该程序从输入文件中解析数据,然后创建某些内存对象,并应编写出使用这些对象的python脚本。 I write these objects in the script file using pickle.dumps() and inserting it into a variable declaration line as per the idea sketched above. 我使用pickle.dumps()在脚本文件中写入这些对象,然后按照上面概述的想法将其插入变量声明行。 But I need to be able to run this code in python 2. 但是我需要能够在python 2中运行此代码。

I did notice that in python 3 the line variable gets each backslash properly escaped and a type: 我确实注意到,在python 3中,line变量正确地转义了每个反斜杠和一个类型:

>>> line
"some_dict_b = b'\\x80\\x02...

while in python 2 I get: 而在python 2我得到:

>>> line
'some_dict_b = \x80\x02...

The Python 3 bytes type doesn't have a string represention, so when converted to a string with %s , the object representation is used instead. Python的3 bytes类型没有字符串表示形式,所以,当转换为一个字符串%s对象表示来代替。 If you wanted to produce Python-compatible syntax from objects, you can use the %r formatter instead, to just use the representation directly. 如果要从对象生成与Python兼容的语法,则可以改用%r格式化程序,直接使用表示形式。

In Python 2: 在Python 2中:

>>> import pickle
>>> some_dict = {'a':0, 'b':1}
>>> p = pickle.dumps(some_dict, 2)
>>> print 'string: %s\nrepresentation: %r' % (p, p)
string: ?}q(UaqKUbqKu.
representation: '\x80\x02}q\x00(U\x01aq\x01K\x00U\x01bq\x02K\x01u.'

In Python 3: 在Python 3中:

>>> import pickle
>>> some_dict = {'a':0, 'b':1}
>>> p = pickle.dumps(some_dict, 2)
>>> print('string: %s\nrepresentation: %r' % (p, p))
string: b'\x80\x02}q\x00(X\x01\x00\x00\x00bq\x01K\x01X\x01\x00\x00\x00aq\x02K\x00u.'
representation: b'\x80\x02}q\x00(X\x01\x00\x00\x00bq\x01K\x01X\x01\x00\x00\x00aq\x02K\x00u.'

Object representations (the output of the repr() function , which uses the object.__repr__ special method ) generally will attempt to provide you with a representation that can be pasted back into a Python script or interactive prompt to recreate the same value. 对象表示形式( repr()函数的输出,它使用object.__repr__特殊方法 )通常会尝试为您提供一种表示形式,该表示形式可以粘贴回Python脚本或交互式提示中以重新创建相同的值。

From the documentation for repr() : repr()的文档中:

For many types, this function makes an attempt to return a string that would yield an object with the same value when passed to eval() , otherwise the representation is a string enclosed in angle brackets that contains the name of the type of the object together with additional information often including the name and address of the object. 对于许多类型,此函数会尝试返回一个字符串,该字符串在传递给eval()时将产生具有相同值的对象,否则表示形式是一个用尖括号括起来的字符串,其中包含对象的类型名称以及通常包括对象名称和地址的其他信息。

None of this is specific to pickle , really. 实际上,这些都不是pickle特有的。

Whenever you think "I use exec", think again. 每当您想到“我使用exec”时,请三思。 You don't. 你不知道 Instead of evaluating data like this, store the contents of the data inside a dict itself. 与其像这样评估数据,不如将数据内容存储在字典本身中。

Then, assign the data explicit to the variable. 然后,将数据显式分配给变量。

some_dict = {'a':0, 'b':1}
line = pickle.dumps(some_dict)
decoded_dict = pickle.loads(line)
decoded_dict == some_dict

You can call repr on the string or bytes object before inserting them into the line. 您可以拨打repr将它们插入到前行的字符串或字节的对象。

# Python 2
>>> 'some_dict = %s' % repr(pickle.dumps(d))
'some_dict = "(dp0\\nS\'a\'\\np1\\nI12\\nsS\'b\'\\np2\\nI24\\ns."'

# Python 3
>>> 'some_dict = %s' % repr(pickle.dumps(d))
"some_dict = b'\\x80\\x03}q\\x00(X\\x01\\x00\\x00\\x00bq\\x01K\\x18X\\x01\\x00\\x00\\x00aq\\x02K\\x0cu.'"

Or use the format method, using !r to automatically call repr : 或者使用format方法,使用!r自动调用repr

>>> 'some_dict = {!r}'.format(pickle.dumps(d))
"some_dict = b'\\x80\\x03}q\\x00(X\\x01\\x00\\x00\\x00bq\\x01K\\x18X\\x01\\x00\\x00\\x00aq\\x02K\\x0cu.'"

(Also works in python 2) (也适用于python 2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM