python 中的酸洗错误?
我收到此错误,但我不知道这意味着什么。我该如何解决这个问题?
我的代码看起来像这样,我以前使用过它并且它有效:
parentdir = os.getcwd()
dirlist = os.listdir(parentdir)
for dir in dirlist:
if not dir == "pubs_edits": continue
if os.path.isdir(os.path.join(parentdir, dir)):
os.chdir(os.path.join(parentdir, dir))
file_list = os.listdir(os.path.join(parentdir, dir))
for f in file_list:
in1 = open(f, 'r')
dict2 = pickle.load(in1)
这是错误消息:
File "/home/md202/pmid_editor.py", line 18, in <module>
dict2 = pickle.load(in1)
File "/usr/lib/python2.5/pickle.py", line 1370, in load
return Unpickler(file).load()
File "/usr/lib/python2.5/pickle.py", line 858, in load
dispatch[key](self)
KeyError: '\x00'
I am getting this error, and I dont know what it means. How can I fix this problem?
my code looks like this, I've used it before and it has worked:
parentdir = os.getcwd()
dirlist = os.listdir(parentdir)
for dir in dirlist:
if not dir == "pubs_edits": continue
if os.path.isdir(os.path.join(parentdir, dir)):
os.chdir(os.path.join(parentdir, dir))
file_list = os.listdir(os.path.join(parentdir, dir))
for f in file_list:
in1 = open(f, 'r')
dict2 = pickle.load(in1)
This is the error message:
File "/home/md202/pmid_editor.py", line 18, in <module>
dict2 = pickle.load(in1)
File "/usr/lib/python2.5/pickle.py", line 1370, in load
return Unpickler(file).load()
File "/usr/lib/python2.5/pickle.py", line 858, in load
dispatch[key](self)
KeyError: '\x00'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
当我尝试解开(使用 pickle.loads)通过 django 存储在数据库中的字符串表示形式时,发生了这个确切的错误。 Django 更改了我的字符串的字符表示,因此
pickle.loads(mystring)
向我抛出了该错误。当我添加显式字符串转换时,效果很好:pickle.loads( str(mystring) )
编辑:查看原始帖子的评论,我认为这与 unicode 字符串问题有关提及。我将一个普通字符串放入数据库,django 返回一个产生此错误的 unicode 字符串。
This exact error occurred for me when I tried to unpickle (using pickle.loads) a string representation that I had stored in a database via django. Django changed the charactee representation of my string so that
pickle.loads(mystring)
threw me that error. When I added an explicit string conversion in, it was fine:pickle.loads( str(mystring) )
EDIT: looking at the comments on the original post, I think this is related to the unicode string issue mentioned. I put a normal string into the database, and django gives me back a unicode string that produces this error.
我遇到了类似的问题,导致
KeyError: '\x1f'
。就我而言,如果被腌制为 gzip 文件(即:
gzip.open(fileName,'wb')
),并且我尝试使用普通文件对象读取它(即:打开(文件名,'rb')
)。I had a similar problem, resulting in
KeyError: '\x1f'
.In my case, if was pickled to a gzip file (ie:
gzip.open(fileName,'wb')
), and I was trying to read it with a normal file object (ie:open(fileName,'rb')
).Pickle 是二进制的,所以你必须这样阅读它。尝试使用 ('rb') 读取二进制文件,而不是 ('r')。另外,如果您编写文件,请确保您也将 pickle 文件写入为二进制文件(“wb”)。这应该有效,希望有帮助。
Pickle is binary so you have to read it as such. Instead of ('r') try using ('rb') read binary. Also, if your writing the file ensure you are wrting the pickle file as binary as well ('wb'). That should work, hope it helps.
尝试
pickle.loads()
try
pickle.loads()
也许你应该尝试另一个协议尝试
pickle.load(in1, 2)
!maybe you should try another protocol try
pickle.load(in1, 2)
!