在不同 Windows 版本上使用 pickle 反序列化日期时间对象
Deserializing datetime objects with pickle on different Windows version
我有一个字典,我想对其进行序列化和反序列化:
dict = {
datetime.datetime.strptime('2016-10-01', '%Y-%m-%d'): {
'product1': 3300.00,
},
datetime.datetime.strptime('2016-10-05', '%Y-%m-%d'): {
'product1': 3000.00,
'product2': 3000.50
},
datetime.datetime.strptime('2016-10-09', '%Y-%m-%d'): {
'product1': 2700.00,
'product2': 2800.50,
'product3': 3600.00
},
datetime.datetime.strptime('2016-10-15', '%Y-%m-%d'): {
'product1': 2500.00,
'product2': 2700.00,
'product4': 666.00
}
}
我正在序列化和反序列化字典使用:
def get_current_datafile():
with open(name='datafile.raw', mode='rb') as input_handle:
input_dict = pickle.loads(input_handle.read())
return input_dict
def write_datafile(new_dict):
with open(name='datafile.raw', mode='wb') as output_handle:
pickle.dump(new_dict, output_handle)
当我在一个环境中使用序列化文件时,它工作正常。但是,当我尝试在另一台机器上反序列化同一个文件时,Python 环境设置完全相同,但它不起作用,给我一个回溯,声称我没有安装 datetime 模块,这当然是不对。
Traceback (most recent call last):
File "scraper.py", line 92, in <module>
Scraper().main()
File "scraper.py", line 32, in main
input_dict = self.get_current_datafile()
File "scraper.py", line 82, in get_current_datafile
input_dict = pickle.loads(input_handle.read())
File "Python\lib\pickle.py", line 1388, in loads
return Unpickler(file).load()
File "Python\lib\pickle.py", line 864, in load
dispatch[key](self)
File "Python\lib\pickle.py", line 1096, in load_global
klass = self.find_class(module, name)
File "Python\lib\pickle.py", line 1130, in find_class
__import__(module)
ImportError: No module named datetime
我所知道的环境之间的唯一区别是 Windows 版本 - 一台机器是 运行:
Python 2.7.12
Pickle r. 72223
Windows 7 x64
另一个是运行:
Python 2.7.12
Pickle r. 72223
Windows 10 x64
问题是:我应该如何配置我的环境,以便我可以在它们之间一致地序列化和反序列化文件?
我刚刚 运行 解决了这个问题。问题是 pickle 文件中的行结尾。 Python windows 下的 2.7.13 只用一个换行符来保存它们。
在第二台机器上,我从 GIT 中检索它们,windows 的 Tortoise GIT 自动将行尾从换行符转换为回车符 Return/Line 喂养。
因此,请确保您的行结尾不会在机器之间转换。一旦我转换回仅换行,导入错误就消失了。
我有一个字典,我想对其进行序列化和反序列化:
dict = {
datetime.datetime.strptime('2016-10-01', '%Y-%m-%d'): {
'product1': 3300.00,
},
datetime.datetime.strptime('2016-10-05', '%Y-%m-%d'): {
'product1': 3000.00,
'product2': 3000.50
},
datetime.datetime.strptime('2016-10-09', '%Y-%m-%d'): {
'product1': 2700.00,
'product2': 2800.50,
'product3': 3600.00
},
datetime.datetime.strptime('2016-10-15', '%Y-%m-%d'): {
'product1': 2500.00,
'product2': 2700.00,
'product4': 666.00
}
}
我正在序列化和反序列化字典使用:
def get_current_datafile():
with open(name='datafile.raw', mode='rb') as input_handle:
input_dict = pickle.loads(input_handle.read())
return input_dict
def write_datafile(new_dict):
with open(name='datafile.raw', mode='wb') as output_handle:
pickle.dump(new_dict, output_handle)
当我在一个环境中使用序列化文件时,它工作正常。但是,当我尝试在另一台机器上反序列化同一个文件时,Python 环境设置完全相同,但它不起作用,给我一个回溯,声称我没有安装 datetime 模块,这当然是不对。
Traceback (most recent call last):
File "scraper.py", line 92, in <module>
Scraper().main()
File "scraper.py", line 32, in main
input_dict = self.get_current_datafile()
File "scraper.py", line 82, in get_current_datafile
input_dict = pickle.loads(input_handle.read())
File "Python\lib\pickle.py", line 1388, in loads
return Unpickler(file).load()
File "Python\lib\pickle.py", line 864, in load
dispatch[key](self)
File "Python\lib\pickle.py", line 1096, in load_global
klass = self.find_class(module, name)
File "Python\lib\pickle.py", line 1130, in find_class
__import__(module)
ImportError: No module named datetime
我所知道的环境之间的唯一区别是 Windows 版本 - 一台机器是 运行:
Python 2.7.12
Pickle r. 72223
Windows 7 x64
另一个是运行:
Python 2.7.12
Pickle r. 72223
Windows 10 x64
问题是:我应该如何配置我的环境,以便我可以在它们之间一致地序列化和反序列化文件?
我刚刚 运行 解决了这个问题。问题是 pickle 文件中的行结尾。 Python windows 下的 2.7.13 只用一个换行符来保存它们。
在第二台机器上,我从 GIT 中检索它们,windows 的 Tortoise GIT 自动将行尾从换行符转换为回车符 Return/Line 喂养。
因此,请确保您的行结尾不会在机器之间转换。一旦我转换回仅换行,导入错误就消失了。