python怎么改变输出顺序_在python中利用dict转json按输入顺序输出内容方式

一般常规的我们保存数据为dict类型时,系统会自动帮我们排序;但有时我们想按照输入顺序的key:value保存到dict中,而不想要改变顺序,则我们可以通过使用collecions,进行排序。

collections是一个python的内建模块。

示例如下:

# -*- coding:utf-8 -*-

#dic = {}

dic = dict()

dic['b'] = 1

dic['a'] = 2

dic['b0'] = 3

dic['a1'] = 4

print("dic is:",dic.items())

import json

jsons = json.dumps(dic)

print("jsons:",jsons)

结果:

('dic is:', [('a', 2), ('a1', 4), ('b', 1), ('b0', 3)])

('jsons:', '{"a": 2, "a1": 4, "b": 1, "b0": 3}')

修改后:

import collections

dic = collections.OrderedDict()

#dic = {}

dic['b'] = 1

dic['a'] = 2

dic['b0'] = 3

dic['a1'] = 4

print("dic is:",dic.items())

import json

jsons = json.dumps(dic)

print("jsons:",jsons)

结果:

('dic is:', [('b', 1), ('a', 2), ('b0', 3), ('a1', 4)])

('jsons:', '{"b": 1, "a": 2, "b0": 3, "a1": 4}')

补充拓展:Python字典转Json并使用多种格式实现

前言:

利用Python数据转换的套路可以遵循:变量定义的位置,字典操作,列表操作,这个三部分的内容可以处理大部分的数据相关需求。

1.下面我们先看这个脚本:

#从字典转换为Json的方法

from distutils.log import warn as printf

from json import dumps

from pprint import pprint

BOOKs = {

'0132269937': {

'title': 'Core Python Programming',

'edition': 2,

'year': 2007,

},

'0132356139': {

'title': 'Python Web Development with Django',

'authors': ['Jeff Forcier', 'Paul Bissex', 'Wesley Chun'],

'year': 2009,

},

'0137143419': {

'title': 'Python Fundamentals',

'year': 2009,

},

}

printf('*** RAW DICT ***')

printf(BOOKs)

printf('n*** PRETTY_PRINTED DICT ***')

pprint(BOOKs)

printf('n*** RAW JSON ***')

printf(dumps(BOOKs))

printf('n*** PRETTY_PRINTED JSON ***')

printf(dumps(BOOKs, indent=4))

输出结果:

"E:Anaconda3 4.2.0python.exe" E:/Pycharm/Python-code/dict2json.py

*** RAW DICT ***

{'0132269937': {'edition': 2, 'title': 'Core Python Programming', 'year': 2007},

'0132356139': {'authors': ['Jeff Forcier', 'Paul Bissex', 'Wesley Chun'],

{'0137143419': {'year': 2009, 'title': 'Python Fundamentals'}, '0132356139': {'year': 2009, 'authors': ['Jeff Forcier', 'Paul Bissex', 'Wesley Chun'], 'title': 'Python Web Development with Django'}, '0132269937': {'year': 2007, 'edition': 2, 'title': 'Core Python Programming'}}

'title': 'Python Web Development with Django',

'year': 2009},

*** PRETTY_PRINTED DICT ***

'0137143419': {'title': 'Python Fundamentals', 'year': 2009}}

*** RAW JSON ***

{"0137143419": {"year": 2009, "title": "Python Fundamentals"}, "0132356139": {"year": 2009, "authors": ["Jeff Forcier", "Paul Bissex", "Wesley Chun"], "title": "Python Web Development with Django"}, "0132269937": {"year": 2007, "edition": 2, "title": "Core Python Programming"}}

*** PRETTY_PRINTED JSON ***

{

"0137143419": {

"year": 2009,

"title": "Python Fundamentals"

},

"0132356139": {

"year": 2009,

"authors": [

"Jeff Forcier",

"Paul Bissex",

"Wesley Chun"

],

"title": "Python Web Development with Django"

},

"0132269937": {

"year": 2007,

"edition": 2,

"title": "Core Python Programming"

}

}

Process finished with exit code 0

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值