python json 编码
2016-12-08 16:55
337 查看
# -*- coding=utf-8 -*- import os import json class info: def __init__(self): self.year = 2014 self.version = '1.0' self.description = 'This is stable 1.0 version of the 2014 MS COCO dataset.' self.contributor = 'Microsoft COCO group' self.url = 'http://mscoco.org' self.date_created = '2015-01-27 09:11:52.357475' def __repr__(self): return repr((self.description, self.url, self.version, self.year, self.contributor, self.date_created)) class image: def __init__(self, id, width, height, file_name, license, flickr_url, coco_url, date_captured): self.id = id self.width = width self.height = height self.file_name = file_name self.license = license self.flickr_url = flickr_url self.coco_url = coco_url self.date_captured = date_captured def __repr__(self): return repr((self.license, self.file_name, self.coco_url, self.height, self.width, self.date_captured, self.flickr_url, self.id)) class license: def __init__(self, id, name, url): self.id = id self.name = name self.url = url def __repr__(self): return repr((self.id, self.name, self.url)) class annotation: def __init__(self, id, image_id, caption): self.id = id self.image_id = image_id self.caption = caption def __repr__(self): return repr((self.id, self.image_id, self.caption)) def dump_json(): info_ = info() images = [image(57870, 640, 480, 'train2014_70.jpg', 5, 'http://farm4.com/z.jpg', 'http://org/57870', '2013-11.14'), image(57871, 641, 481, 'train2014_71.jpg', 6, 'http://farm4.com/a.jpg', 'http://org/57871', '2013-11-15'), ] annotations = [annotation(48, 57870, 'A very clean and well decorated empty bathroom'), annotation(49, 57871, 'A panoramic view of a kitchen and all of its applications'), ] licenses = [license(5, 'A', 'http://mscoco.org'), license(6, 'B', 'http://mscoco.org'), ] # bigD = {"info": info.__dict__, "images": images.__dict__, "annotations": annotations.__dict__, "licenses": licenses.__dict__} bigD = {"info": info_, "images": images, "annotations": annotations, "licenses": licenses} json_str = json.dumps(bigD, default=lambda o: o.__dict__, sort_keys=True, indent=4) # json_str = json.dumps(bigD) print(json_str) def put_data_into_json(): def main(): # dump_json() if __name__ == '__main__': main()
相关文章推荐
- Python中json.loads的时候出错->要注意要解码的Json字符的编码
- python中json.dumps使用及字符编码
- Python基础-JSON编码与解码
- Python爬取新浪英超曼联文章内页--bs4,json,txt和csv以及编码
- Python中json格式数据的编码和解码
- python字符编码问题以及json编码问题
- python中对于json写入txt文件的编码问题
- Python中json格式数据的编码与解码方法详解
- Python处理JSON时的值报错及编码报错的两则解决实录
- python json.dumps 中文编码问题
- python json 序列化反序列化 和 中文编码问题
- python读取中文gbk编码json文件
- Python 2.7.9 Demo - JSON的编码、解码
- Python中json格式数据的编码与解码方法详解
- JSON: Python Objects与String之间转换【json.dumps把python任意数据类型转换成字符串,json.loads把字符串转换为unicode编码】
- Python中json格式数据的编码和解码
- Python中json格式数据的编码和解码
- Python中JSON在解析的同时进行自定义编码处理
- 关于python中json load出来编码为unicode的问题的解决
- python2.7中关于编码,json格式的中文输出显示