当前位置:网站首页>学习总结week4_1json
学习总结week4_1json
2022-08-10 02:57:00 【非鱼丶丶】
python文件操作-json
服务端java/php/python/go
ios OC swift
安卓 java
windows C#
Mac OC swift
网页客户端 js
通用数据格式:json、xml
一、json
json和xml 都是通用的数据格式,可以用于不同编程语言之间的数据交流
json相对xml更小更轻,传输数据更快,保存在文件里也更新
xml相对json更安全
1.json数据格式
json数据格式的要求:一个json有且只有一个数据;唯一的这个数据必须是json支持的类型的数据
json支持的类型:
1.数字: 包括整数和小数,表示的时候直接写:1、23.5、-5
2.字符串: 用双引号引起来的数据"3451"、“aa\n155\u4e00”
3.布尔: 只有ture 和 false 两个值
4.空值: null
5.数组: 相当于python的列表:[数据1, 数据2, …]
6.字典: python的字典,但是键只能是字符串 “”
2.python和json数据之间的相互转换
python中提供了json模块专门来处理python中的json问题
- json转python
json -> python
数字 数字
字符串 字符串
布尔 布尔(true-> True、false->False)
空值 空值(null->None)
数组 列表
字典 字典
对应的函数:json.loads(json格式字符串) - 将json格式字符串对应的json数据转换成python数据
注意:json格式字符串指的是字符串内容是json内容是json数据的字符串
res = open('test.json', encoding='utf-8').read()
res1 = json.loads(res)
print(res1)
res1 = json.loads('[true, null, false, 1, "oo"]')
print(res1)
# 示例:从网页接口中解析数据获取所有英雄的名字
# 1.获取json数据(从文件中读出来、直接做网络请求)
import requests
response = requests.get('https://game.gtimg.cn/images/lol/act/img/js/heroList/hero_list.js?ts=2766570')
content = response.text
print(type(content), content)
# 2. json解析(将json数据转化成对应的Python数据)
result = json.loads(content)
for x in result['hero']:
print(x['name'], x['title'])
2)python转json
python -> json
int、float 数字
str 字符串
bool True-> true、False->false
None null
列表、元组 数组
字典 字典(键会变为字符串)
对应函数: json.dumps(python数据) - 将指定python数据转换成对应的json格式字符串
res = json.dumps(‘100’)
print(res)
res = json.dumps('100')
print(res)
3.实际应用
=========添加学生==========
请输入学生姓名:stu1
请输入学生的年龄:19
请输入学生的手机号:110
请输入学生的专业:电子信息
请输入学生的籍贯:重庆
添加成功!
️1. 继续
️2. 退出
请选择:1
=========添加学生==========
请输入学生姓名:stu2
请输入学生的年龄22
请输入学生的手机号:3332
请输入学生的专业:电子信息
请输入学生的籍贯:重庆
添加成功!
️1. 继续
️2. 退出
请选择:1
=========添加学生==========
请输入学生姓名:stu2
请输入学生的年龄:22
请输入学生的手机号:3332
请输入学生的专业:电子信息
请输入学生的籍贯:重庆
添加成功!
️1. 继续
️2. 退出
请选择:2
(显示已经添加的所有的学生信息!)
(程序结束)
# 数据存储思路
1. 已经添加过的所有学生需要持久化
2. 文件内容格式:
[
{"name": "小明", "age": 18, "major":"电子信息", "address": "成都"},
...
]
import json
def add_student():
while True:
# 1. 输入学生信息
print('=========添加学生==========')
name = input('请输入学生姓名:')
age = input('请输入学生的年龄:')
tel = input('请输入学生的电话:')
major = input('请输入学生的专业:')
address = input('请输入学生的籍贯:')
# 2. 保存学生信息
content = open('files/student.txt', encoding='utf-8').read() # '[]'
all_students = json.loads(content) # type: list
# [{}]
all_students.append({
'name': name, 'age': age, 'tel': tel, 'major': major, 'address': address})
open('files/student.txt', 'w', encoding='utf-8').write(json.dumps(all_students))
print('添加成功!')
# 3. 提示继续或者退出
print('️1. 继续')
print('️2. 退出')
value = input('请选择:')
if value == '1':
pass
else:
# 打印已经添加过的所有的学生信息
print(all_students)
break
if __name__ == '__main__':
# add_student()
# 统计电子专业学生的人数:
all_student = json.loads(open('files/student.txt', encoding='utf-8').read())
count = 0
for x in all_student:
if x['major'] == '电子':
count += 1
print(count)
边栏推荐
猜你喜欢

【CC3200AI 实验教程5】疯壳·AI语音人脸识别(会议记录仪/人脸打卡机)-定时器

国能准能集团研发矿山数字孪生系统 填补国内采矿行业空白

HackTheBox——Beep

Recommend several easy-to-use MySQL open source clients, it is recommended to collect

兴业数金一面

vue项目 npm run build 打包项目防止浏览器缓存

【图像分类】2022-CycleMLP ICLR

论文理解:“PIAT: Physics Informed Adversarial Training for Solving Partial Differential Equations“

(面试加分新技能) 总结11个ES2022中你可能遗漏的语法

使用curl指令发起websocket请求
随机推荐
State compression small experience
Basic understanding of network models
嵌入式分享合集32
从8k到13k,我全靠这本《接口自动化测试——从入门到精通》
基础理解网络模型
exchange2010 邮件数据库无法装入
PostgreSQL相关语法及指令示例
Software life cycle (the work of each phase of software engineering)
驱动程序开发:按键中断之异步通知
“双枪”木马病毒的进化史
Difference between netstat and ss command
Example 048: Number ratio size
(十四)时间延时任务及定时任务
【语义分割】2022-HRViT CVPR
Chapter 21 Source Code File REST API Reference (3)
【Image Classification】2022-CycleMLP ICLR
HRnet
二维空间下的向量旋转
flutter 制作嵌套列表
软件测试这些基本类型你知道吗?