Python中如何处理json数据?

2026-05-21 17:591阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计197个文字,预计阅读时间需要1分钟。

Python中如何处理json数据?

加载数本地的json文件,并删除其中一项元素:使用`with open('./test.json', 'r') as f: data=json.load(f)`读取文件,然后通过`del data['key']`删除指定键对应的元素。

加载本地json:

with open('./test.json', 'r') as f:
data = json.load(f)


删除json中一个元素# -*- coding=utf8 -*-
import io
import urllib2
from urllib2 import urlopen
from PIL import Image
import json


if __name__ == '__main__':
with open('j', 'r') as f:
data = json.load(f)

ll = data['list']
for user in ll:
if user['url'] == '5348728':
print 'find it!'
ll.remove(user)


for user in data['list']:
if user['url'] == '5348728':
print 'find it!'

with open('j', 'w') as f:
f.write(json.dumps(data))

Python中如何处理json数据?

黄世宇/Shiyu Huang's Personal Page:​​huangshiyu13.github.io/​​



本文共计197个文字,预计阅读时间需要1分钟。

Python中如何处理json数据?

加载数本地的json文件,并删除其中一项元素:使用`with open('./test.json', 'r') as f: data=json.load(f)`读取文件,然后通过`del data['key']`删除指定键对应的元素。

加载本地json:

with open('./test.json', 'r') as f:
data = json.load(f)


删除json中一个元素# -*- coding=utf8 -*-
import io
import urllib2
from urllib2 import urlopen
from PIL import Image
import json


if __name__ == '__main__':
with open('j', 'r') as f:
data = json.load(f)

ll = data['list']
for user in ll:
if user['url'] == '5348728':
print 'find it!'
ll.remove(user)


for user in data['list']:
if user['url'] == '5348728':
print 'find it!'

with open('j', 'w') as f:
f.write(json.dumps(data))

Python中如何处理json数据?

黄世宇/Shiyu Huang's Personal Page:​​huangshiyu13.github.io/​​