内容简介:Python内置了json包来帮助我们完成对json的操作。针对str操作:将Python的字典结构导出到json使用json.dumps() ,将json读成Python的字典结构,使用json.loads() 。针对对文件操作:分别使用json.load()函数和json.dump()函数。
使用 Python 编码和解析Json
Python内置了json包来帮助我们完成对json的操作。
针对str操作:将Python的字典结构导出到json使用json.dumps() ,将json读成Python的字典结构,使用json.loads() 。
针对对文件操作:分别使用json.load()函数和json.dump()函数。
# coding=gbk
import traceback
import json
file_name = '广州市_农贸市场_1717.txt'
f = open(file_name, 'r')
tmp = json.load(f)
print(type(tmp))
data = []
for i in tmp:
# print(i)
#测试做一下格式转换
dl = {
"name":i["name"],
"location_lat" : i["location"]["lat"],
"location_lng" : i["location"]["lng"]
}
data.append(dl)
print(data)
with open('ceshi.txt', 'w', encoding="utf-8") as f:
f.write(json.dumps(data, ensure_ascii=False))
print('---wirte success---')
附上 广州市_农贸市场_1717.txt
[{"name": "江南市场", "location": {"lat": 23.179127, "lng": 113.236457}, "address": "广东省广州市白云区增槎路926号", "province": "广东省", "city": "广州市", "area": "白云区", "street_id": "9fbfeef24609615cece4a59b", "detail": 1, "uid": "9fbfeef24609615cece4a59b"}, {"name": "广州番禺清河市场", "location": {"lat": 22.938501, "lng": 113.401308}, "address": "广东省广州市番禺区亚运大道94号", "province": "广东省", "city": "广州市", "area": "番禺区", "street_id": "bc57b5426b43342922f01731", "telephone": "(020)84629663", "detail": 1, "uid": "bc57b5426b43342922f01731"}]
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- JavaScript进阶系列-类型转换、隐式类型转换
- Android 多国语言转换 Excel 和 Excel 转换为 string
- [SSL证书转换(一)]关于JKS 转换成 CRT 和 KEY
- c++中几种常见的类型转换。int与string的转换,float与string的转换以及string和long类型之间的相互...
- Protocol Buffer使用转换工具将proto文件转换成Java文件流程及使用
- 开源 | Alita:一套把 React Native 代码转换成微信小程序代码的转换引擎工具
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
HTTP Essentials
Stephen A. Thomas、Stephen Thomas / Wiley / 2001-03-08 / USD 34.99
The first complete reference guide to the essential Web protocol As applications and services converge and Web technologies not only assume HTTP but require developers to manipulate it, it is be......一起来看看 《HTTP Essentials》 这本书的介绍吧!
在线进制转换器
各进制数互转换器
RGB CMYK 转换工具
RGB CMYK 互转工具