腌制文本文件导致ValueError(dos / unix问题) [英] Pickled text file causing ValueError (dos/unix issue)

查看:81
本文介绍了腌制文本文件导致ValueError(dos / unix问题)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我开始使用pickle来存储工具的最新用户设置

我写的。当它终止时它会写出一个烂蛋文件。

在启动时恢复设置。


效果非常好。


但是,当我从Unix启动该工具时,我得到了一个ValueError,因为我之前使用过Windows中的工具。


文件& ; /usr/local/lib/python2.3/pickle.py" ;,第980行,在load_string中

引发ValueError,不安全的字符串pickle

ValueError:insecure string pickle


如果我做''dos2unix< my.cfg> < my.cfg>''转换文件,然后

一切

变好了。


我在Python发行说明说......

" pickle:当一个无效的pickle包含一个

非字符串repr,其中包含一个字符串repr时,会引发ValueError。这个行为匹配

cPickle。"


我猜DOS文本格式正在创建这个问题。

我的问题是有没有什么优雅的方法可以解决这个问题?


我当然可以捕获ValueError并明确地运行''dos2unix'。

但我不喜欢不喜欢这样粗暴的解决方案。

任何建议都会受到高度赞赏。


祝你好运,

Aki Niimura

Hello everyone,

I started to use pickle to store the latest user settings for the tool
I wrote. It writes out a pickled text file when it terminates and it
restores the settings when it starts.

It worked very nicely.

However, I got a ValueError when I started the tool from Unix when I
previously used the tool from Windows.

File "/usr/local/lib/python2.3/pickle.py", line 980, in load_string
raise ValueError, "insecure string pickle"
ValueError: insecure string pickle

If I do ''dos2unix <my.cfg> <my.cfg>'' to convert the file, then
everything
becomes fine.

I found in the Python release note saying ...
"pickle: Now raises ValueError when an invalid pickle that contains a
non-string repr where a string repr was expected. This behavior matches
cPickle."

I guess DOS text format is creating this problem.
My question is "Is there any elegant way to deal with this?".

I certainly can catch ValueError and run ''dos2unix'' explicitly.
But I don''t like such crude solution.
Any suggestions would be highly appreciated.

Best regards,
Aki Niimura

推荐答案

在Windows上打开文件,用wb写入模式,b代表二进制。
Open the file on windows for writing with "wb" mode, the b is for binary.


[Aki Niimura]
[Aki Niimura]
我开始使用pickle存储工具的最新用户设置<我写道。它在终止时写出一个酸洗文本文件,并在启动时恢复设置。
....我猜DOS文本格式正在创建这个问题。


是的。

我的问题是有没有优雅的方法来解决这个问题?。
I started to use pickle to store the latest user settings for the tool
I wrote. It writes out a pickled text file when it terminates and it
restores the settings when it starts. .... I guess DOS text format is creating this problem.
Yes.
My question is "Is there any elegant way to deal with this?".




是的:无论平台如何,总是在

二进制模式下打开用于泡菜的文件。也就是说,传递rb。在阅读pickle文件时打开(),

和wb写一个pickle文件时打开()然后你的pickle文件

将在所有平台上保持不变。对于包含任何类型的二进制数据的文件来说也是如此(尽管有多种泡菜协议0

被称为文本模式多年,它仍然是二进制数据)。



Yes: regardless of platform, always open files used for pickles in
binary mode. That is, pass "rb" to open() when reading a pickle file,
and "wb" to open() when writing a pickle file. Then your pickle files
will work unchanged on all platforms. The same is true of files
containing binary data of any kind (and despite that pickle protocol 0
was called "text mode" for years, it''s still binary data).


Tim Peters写道:
Tim Peters wrote:
是的:无论平台如何,总是打开用于泡菜的文件二进制模式。也就是说,传递rb。在阅读pickle文件时打开(),
和wb写一个pickle文件时打开()那么你的pickle文件
将在所有平台上保持不变。包含任何类型的二进制数据的文件也是如此(尽管多年来该文章模式被称为文本模式,但它仍然是二进制数据)。
Yes: regardless of platform, always open files used for pickles in
binary mode. That is, pass "rb" to open() when reading a pickle file,
and "wb" to open() when writing a pickle file. Then your pickle files
will work unchanged on all platforms. The same is true of files
containing binary data of any kind (and despite that pickle protocol 0
was called "text mode" for years, it''s still binary data).




我一直想知道为什么在二元模式

和文本模式之间进行选择。为什么我们不能放弃''文本模式'?

它做了什么,反正呢?至少,如果它做了什么,我肯定

如果真的需要这样做,那么它本身就不能用Python本身完成。 ..


- 图片



I''ve been wondering why there even is the choice between binary mode
and text mode. Why can''t we just do away with the ''text mode'' ?
What does it do, anyways? At least, if it does something, I''m sure
that it isn''t something that can be done in Python itself if
really required to do so...

--Irmen


这篇关于腌制文本文件导致ValueError(dos / unix问题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆