• <i id='I25nN'><tr id='I25nN'><dt id='I25nN'><q id='I25nN'><span id='I25nN'><b id='I25nN'><form id='I25nN'><ins id='I25nN'></ins><ul id='I25nN'></ul><sub id='I25nN'></sub></form><legend id='I25nN'></legend><bdo id='I25nN'><pre id='I25nN'><center id='I25nN'></center></pre></bdo></b><th id='I25nN'></th></span></q></dt></tr></i><div id='I25nN'><tfoot id='I25nN'></tfoot><dl id='I25nN'><fieldset id='I25nN'></fieldset></dl></div>

  • <tfoot id='I25nN'></tfoot>
    <legend id='I25nN'><style id='I25nN'><dir id='I25nN'><q id='I25nN'></q></dir></style></legend>
    1. <small id='I25nN'></small><noframes id='I25nN'>

          <bdo id='I25nN'></bdo><ul id='I25nN'></ul>

        如何用 Python 保存数据?

        How to save data with Python?(如何用 Python 保存数据?)

          <tfoot id='n5V3G'></tfoot>

            <small id='n5V3G'></small><noframes id='n5V3G'>

              <bdo id='n5V3G'></bdo><ul id='n5V3G'></ul>
            • <i id='n5V3G'><tr id='n5V3G'><dt id='n5V3G'><q id='n5V3G'><span id='n5V3G'><b id='n5V3G'><form id='n5V3G'><ins id='n5V3G'></ins><ul id='n5V3G'></ul><sub id='n5V3G'></sub></form><legend id='n5V3G'></legend><bdo id='n5V3G'><pre id='n5V3G'><center id='n5V3G'></center></pre></bdo></b><th id='n5V3G'></th></span></q></dt></tr></i><div id='n5V3G'><tfoot id='n5V3G'></tfoot><dl id='n5V3G'><fieldset id='n5V3G'></fieldset></dl></div>
                    <tbody id='n5V3G'></tbody>

                1. <legend id='n5V3G'><style id='n5V3G'><dir id='n5V3G'><q id='n5V3G'></q></dir></style></legend>
                  本文介绍了如何用 Python 保存数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在编写一个 Python 程序,并希望用户能够保存他们正在处理的数据.我研究过 cPickle;看起来这将是一种快速简便的数据保存方法,但似乎不安全.由于可以腌制整个函数、类等,我担心流氓保存文件可能会将有害代码注入程序.有没有办法可以防止这种情况发生,或者我应该研究其他保存数据的方法,例如直接转换为字符串(这似乎也不安全)或创建 XML 层次结构,然后将数据放入其中.

                  I am working on a program in Python and want users to be able to save data they are working on. I have looked into cPickle; it seems like it would be a fast and easy way to save data, it seems insecure. Since entire functions, classes, etc can be pickled, I am worried that a rogue save file could inject harmful code into the program. Is there a way I can prevent that, or should I look into other methods of saving data, such as directly converting to a string (which also seems insecure,) or creating an XML hierarchy, and putting data in that.

                  我是python新手,请多多包涵.

                  I am new to python, so please bear with me.

                  提前致谢!

                  至于我存储的数据类型,主要是字典和列表.名称、速度等信息.目前相当简单,但未来可能会变得更复杂.

                  As for the type of data I am storing, it is mainly dictionaries and lists. Information such as names, speeds, etc. It is fairly simple right now, but may get more complex in the future.

                  推荐答案

                  从您的描述来看,JSON 编码是安全快速的解决方案.python2.6中有一个json模块,可以这样使用:

                  From your description JSON encoding is the secure and fast solution. There is a json module in python2.6, you can use it like this:

                  import json
                  obj = {'key1': 'value1', 'key2': [1, 2, 3, 4], 'key3': 1322}
                  encoded = json.dumps(obj)
                  obj = json.loads(encoded)
                  

                  JSON 格式是人类可读的,与 python 中的字典字符串表示非常相似.并且没有任何像泡菜这样的安全问题.如果没有python2.6可以安装cjson或者simplejson

                  JSON format is human readable and is very similar to the dictionary string representation in python. And doesn't have any security issues like pickle. If you don't have python2.6 you can install cjson or simplejson

                  您不能使用 JSON 来保存像 Pickle 这样的 Python 对象.但是你可以用它来保存:字符串、字典、列表……对于大多数情况来说已经足够了.

                  You can't use JSON to save python objects like Pickle. But you can use it to save: strings, dictionaries, lists, ... It can be enough for most cases.

                  解释为什么pickle不安全.来自python 文档:

                  大多数安全问题围绕泡菜和cPickle模块涉及 unpickling.有没有已知的安全漏洞与酸洗有关,因为你(程序员)控制的对象pickle 将与之交互,以及所有它产生的是一个字符串.

                  Most of the security issues surrounding the pickle and cPickle module involve unpickling. There are no known security vulnerabilities related to pickling because you (the programmer) control the objects that pickle will interact with, and all it produces is a string.

                  但是,对于 unpickling,它从不解开不受信任的好主意来源可疑的字符串,因为例如,从套接字读取的字符串.这是因为 unpickling 可以创建意想不到的物体,甚至可能运行那些方法对象,例如它们的类构造函数或析构函数... 这个故事的寓意是你应该非常小心您的应用程序的字符串来源解封.

                  However, for unpickling, it is never a good idea to unpickle an untrusted string whose origins are dubious, for example, strings read from a socket. This is because unpickling can create unexpected objects and even potentially run methods of those objects, such as their class constructor or destructor ... The moral of the story is that you should be really careful about the source of the strings your application unpickles.

                  有一些方法可以保护自己,但在你的情况下使用 JSON 更容易.

                  There are some ways to defend yourself but it is much easier to use JSON in your case.

                  这篇关于如何用 Python 保存数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Running .jl file from R or Python(从 R 或 Python 运行 .jl 文件)
                  Running Julia .jl file in python(在 python 中运行 Julia .jl 文件)
                  Using PIP in a Azure WebApp(在 Azure WebApp 中使用 PIP)
                  How to run python3.7 based flask web api on azure(如何在 azure 上运行基于 python3.7 的烧瓶 web api)
                  Azure Python Web App Internal Server Error(Azure Python Web 应用程序内部服务器错误)
                  Run python dlib library on azure app service(在 azure app 服务上运行 python dlib 库)
                      <tbody id='MNjGW'></tbody>

                    <small id='MNjGW'></small><noframes id='MNjGW'>

                    <i id='MNjGW'><tr id='MNjGW'><dt id='MNjGW'><q id='MNjGW'><span id='MNjGW'><b id='MNjGW'><form id='MNjGW'><ins id='MNjGW'></ins><ul id='MNjGW'></ul><sub id='MNjGW'></sub></form><legend id='MNjGW'></legend><bdo id='MNjGW'><pre id='MNjGW'><center id='MNjGW'></center></pre></bdo></b><th id='MNjGW'></th></span></q></dt></tr></i><div id='MNjGW'><tfoot id='MNjGW'></tfoot><dl id='MNjGW'><fieldset id='MNjGW'></fieldset></dl></div>

                            <bdo id='MNjGW'></bdo><ul id='MNjGW'></ul>
                            <tfoot id='MNjGW'></tfoot>

                            <legend id='MNjGW'><style id='MNjGW'><dir id='MNjGW'><q id='MNjGW'></q></dir></style></legend>