当前位置:网站首页>学习总结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)
边栏推荐
- 怎么进行服务器性能监控,有什么监控工具
- matlab simulink响应谱计算
- It's almost 35, still "did a little"?What happened to the test workers who had been in the industry for a few years?
- Arrays类
- Chip Accelerator
- [Kali Security Penetration Testing Practice Course] Chapter 7 Privilege Escalation
- 三极管开关电路参数设计与参数介绍
- 【图像分类】2022-ResMLP
- Mini Program Navigation and Navigation Parameters
- 6 common plugin recommendations in Pycharm
猜你喜欢
随机推荐
使用flink-sql写入mysql的时候,只指定插入的字段,但是会报错id字段错误,没有默认值,创
QT modal dialog and non-modal dialog learning
过水滑环的结构和工作原理
Arrays类
成功执行数字化转型的9个因素
HACKTHEBOX——Bank
是什么让训练综合分类网络艰苦?
flutter 制作嵌套列表
Example 043: Scope, class methods and variables
Instance 042: Variable scope
网页挖矿溯源?浏览器浏览历史查看工具Browsinghistoryview
量化投资学习——在FPGA上运行高频交易策略
兴业数金一面
leetcode-218.天际线问题
Pen paper records
State compression small experience
proxy代理服务
zabbix添加监控主机和自定义监控项
C - The Battle of Chibi (dp加树状数组前缀和优化)
uni-app自定义导航栏









