zoukankan      html  css  js  c++  java
  • Saving structured data with json

    Strings can easily be written to and read from a file. Numbers take a bit more effort, since the read() method only returns strings, which will have to be passed to a function like int(), which takes a string like '123' and returns its numeric value 123. When you want to save more complex data types like nested lists and dictionaries, parsing and serializing by hand becomes complicated.

    Rather than having users constantly writing and debugging code to save complicated data types to files, Python allows you to use the popular data interchange format called JSON (JavaScript Object Notation). The standard module called json can take Python data hierarchies, and convert them to string representations; this process is called serializing. Reconstructing the data from the string representation is called deserializing. Between serializing and deserializing, the string representing the object may have been stored in a file or data, or sent over a network connection to some distant machine.

    Note

     

    The JSON format is commonly used by modern applications to allow for data exchange. Many programmers are already familiar with it, which makes it a good choice for interoperability.

    If you have an object x, you can view its JSON string representation with a simple line of code:

    >>>
    >>> json.dumps([1, 'simple', 'list'])
    '[1, "simple", "list"]'
    

    Another variant of the dumps() function, called dump(), simply serializes the object to a text file. So if f is a text file object opened for writing, we can do this:

    json.dump(x, f)
    

    To decode the object again, if f is a text file object which has been opened for reading:

    x = json.load(f)
    

    This simple serialization technique can handle lists and dictionaries, but serializing arbitrary class instances in JSON requires a bit of extra effort. The reference for thejson module contains an explanation of this.

    See also

     

    pickle - the pickle module

    Contrary to JSONpickle is a protocol which allows the serialization of arbitrarily complex Python objects. As such, it is specific to Python and cannot be used to communicate with applications written in other languages. It is also insecure by default: deserializing pickle data coming from an untrusted source can execute arbitrary code, if the data was crafted by a skilled attacker.

  • 相关阅读:
    D3.js 几种常用的坐标轴
    前端页面的用户体验优化设计
    用can-fixture拦截Ajax并模拟响应
    webpack入门及使用
    CommonJS和AMD规范
    凝思6.0虚拟机搭建--遇到的问题
    凝思6.0安装vmware tools记录
    linux定时任务crontab命令
    find命令使用指南
    关于字体、字形、字符集、字体大小
  • 原文地址:https://www.cnblogs.com/dltts/p/5985231.html
Copyright © 2011-2022 走看看