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.

Saving structured data with json的更多相关文章

  1. Introduction to Structured Data json的2种形式 JAVA解析JSON数据 - JsonArray JsonObject

    https://developers.google.com/search/docs/guides/intro-structured-data Structured data refers to kin ...

  2. <Spark><Programming><Loading and Saving Your Data>

    Motivation Spark是基于Hadoop可用的生态系统构建的,因此Spark可以通过Hadoop MapReduce的InputFormat和OutputFormat接口存取数据. Spar ...

  3. Introduction to Structured Data

    https://developers.google.com/search/docs/guides/intro-structured-data Structured data refers to kin ...

  4. SoapUI 设置 request data with json body

    --背景 使用WCF定义REST风格的WebService,如下: [ServiceContract]    public interface INISTService    {        [Op ...

  5. Bigtable: A Distributed Storage System for Structured Data

    https://static.googleusercontent.com/media/research.google.com/en//archive/bigtable-osdi06.pdf Abstr ...

  6. Python-requests之POST Data的json问题

    代码如下: import json import requests r = requests.post(url, data = {"a": json.dumps({"b& ...

  7. ethereum/EIPs-712 Ethereum typed structured data hashing and signing

    https://github.com/ethereum/EIPs/blob/master/EIPS/eip-712.md eip title author discussions-to status ...

  8. $.post(url,[data],[callback],'json')

    $.post(url,[data],[callback],'json')这个写法来做到用post方法传递数据,并取加回json型数据.如果我要取回的数据类型是xml的,就可以写成$.post(url, ...

  9. Python requests模块params、data、json的区别

    json和dict对比 json的key只能是字符串,python的dict可以是任何可hash对象(hashtable type): json的key可以是有序.重复的:dict的key不可以重复. ...

随机推荐

  1. JavaWeb基础:Servlet Response

    ServletResponse简介 ServletResponse代表浏览器输出,它提供所有HttpResponse的设置接口,可以设置HttpResponse的响应状态,响应头和响应内容. 生命周期 ...

  2. Loadrunner基础:Loadrunner Controller基本概念和使用

    Loadrnner Controller 介绍 当Vuser脚本开发完成以后,可以使用Controller将这个执行脚本的用户从单用户转化为多用户,从而模拟大量用户的操作,形成负载(多用户单循环,多用 ...

  3. python中read、readline和readlines

    f  = file('test.txt' , 'r') all_data = f.read()   # 字符串类型 f.read(8)                         #读取固定大小的 ...

  4. lucene Lock obtain timed out: Lock@

    出错界面: 解决办法: 出现以上异常主要有两种原因: 1.系统正在写索引未完成之前,应用程序关闭 解决方法:删除提示的lock文件后重启应用(最好在应用中捕捉到,自动删除) 2.系统中有多个线程或程序 ...

  5. php提示Fatal error: Call to undefined function imagecreate()

    在php中imagecreate函数是一个图形处理函数,主要用于新建一个基于调色板的图像了,然后在这个基础上我们可以创建一些图形数字字符之类的,但这个函数需要GD库支持,如果没有开启GD库使用时会 / ...

  6. 支持新版chrome,用webstorm编译形成css和sourcemap,调试sass和less源文件(转)

    旧版的chrome有个support for sass,但是新版chrome没有这个功能了.看到网上提供的方法比较多,也很乱,旧版新版的都有.而且不能指定自己所需要的路径. 所以就做了下改版. sas ...

  7. 230. Kth Smallest Element in a BST ——迭代本质:a=xx1 while some_condition: a=xx2

    Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Not ...

  8. tomcat 创建虚拟主机

    1. tomcat8 2. TOMCATROOT/conf/server.xml 增加<Host name="HOSTNAME" appBase="ROOTDir& ...

  9. linux系统设置静态IP 查看网卡配置文件

    http://jingyan.baidu.com/article/455a99508be7cda167277865.html vi /etc/sysconfig/network-scripts/ifc ...

  10. mark 一下

    Android资源管理框架(Asset Manager)简要介绍和学习计划 http://www.cnblogs.com/hjtdlx/p/4332060.html