python笔记基础--文件和存储数据(7)

目录

1.从文件中读取数据

2.写入文件

3.存储数据

3.1使用json.dump()和json.load()

3.2保存和读取用户生成的数据

3.3重构


1.从文件中读取数据

|--------|-----------------------------------------------------------------------------------------------------------------------------|
| 读取整个文件 | with open('data.txt') as file_object: contents = file_object.read() print(contents) print(contents.rstrip()) #删除字符串末尾空白 |
| 文件路径 | with open('text_files//data.txt') as file_object: |
| 文件路径 | file_path = '/home/eh/data.txy' with open(file_path) as file_object: |
| 逐行读取 | with open(file_path) as file_object: for line in file_path: print(line.rstrip()) |

|-------------------------------|----------------------------------------------------------------------------------------------------------------------------------|
| 创建一个包含 文件各行内容的列表 | filename ='data.txt' with open(filename) as file_object: lines = file_object.readlines() for line in lines: print(line.rstrip()) |
| 使用文件内容 | data_string = '' for line in lines: data_string += line.rstrip() #循环加入data_string print(data_string) |
| 打印圆周率小数点后10位数 3.1415926535... | print(f"{data_string[:12]}...") |
| 数列中是否包含这个字段 | ...... my_data = input("输入数字,字符串是否有这个数:") if my_data in data_string: print("你的数字在这个字符串中") else: print("你的数字不在这个字符串中") |

2.写入文件

|----------------------|-------------------------------------------------------------------------------------------------------|
| 写入空文件 | file = 'abc.txt' with open(file) as file_object: file_object.write("我是一个蘑菇。") |
| 写入多行open(file, 'w') | with open(file, 'w') as file_object: file_object.write("我是一个蘑菇。\n") file_object.write("我不是一个蘑菇。\n") |
| 附加到文件open(file, 'a') | with open(file, 'a') as file_object: file_object.write("我是一个蘑菇。\n") |

3.存储数据

json能够将简单的python数据结构转储到文件中

3.1使用json.dump()和json.load()

|-------------|------------|------------------------------------------------------------------------------------------------------------|-----------------------------|
| json.dump() | 可以接受两个实参 | import json number =[2,3,5] filename = 'number.json' with open(filename, 'w') as f: json.dump(number, f) | number.json文件中: [2, 3, 5] |
| json.load() | 将列表 读取到内存中 | with open(filename, 'w') as f: numbers = json.load(f) print(numbers) | |

3.2保存和读取用户生成的数据

|-----------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------|---------------------|-----------------|
| | 代码 | 输出 | username.json文件 |
| 存储用户 | import json username = input("你的名字:") filename = 'username.json' with open(filename, 'w') as f: json.dump(username, f) print(f"Hello,{username}!") | 你的名字:Ann Hello,Ann! | "Ann" |
| 向已存储的用户 发出问候 | import json filename = 'username.json' with open(filename) as f: username = json.load(f) print(f"欢迎回来,{username}!") | 欢迎回来,Ann! | |
| 如用户已存储,加载他。如果没有,则提示用户输入并存储。 | import json filename = 'username.json' try: with open(filename) as f: #尝试打开username.json username = json.load(f) #如果username.json存在 except FileNotFoundError: #username.json不存在 username = input("你的名字:") with open(username, 'w') as f: json.dump(username, f) #存储 print(f"Hello,{username}!") else: print(f"欢迎回来,{username}!") |||

3.3重构

****重构:****代码能够正常运行,但通过将其划分为一系列完成具体工作的函数,还可以进行改进。

****作用:****让代码更清晰、更利于理解、更容易扩展。

相关推荐
大模型铲屎官35 分钟前
【Python-Day 14】玩转Python字典(上篇):从零开始学习创建、访问与操作
开发语言·人工智能·pytorch·python·深度学习·大模型·字典
yunvwugua__38 分钟前
Python训练营打卡 Day27
开发语言·python
Stara05112 小时前
基于多头自注意力机制(MHSA)增强的YOLOv11主干网络—面向高精度目标检测的结构创新与性能优化
人工智能·python·深度学习·神经网络·目标检测·计算机视觉·yolov11
Java致死2 小时前
设计模式Java
java·开发语言·设计模式
zh_xuan2 小时前
c++ 类的语法3
开发语言·c++
Lester_11012 小时前
嵌入式学习笔记 - STM32 ADC 模块工作模式总结
笔记·学习
那雨倾城3 小时前
使用 OpenCV 将图像中标记特定颜色区域
人工智能·python·opencv·计算机视觉·视觉检测
belldeep5 小时前
如何阅读、学习 Tcc (Tiny C Compiler) 源代码?如何解析 Tcc 源代码?
c语言·开发语言
LuckyTHP5 小时前
java 使用zxing生成条形码(可自定义文字位置、边框样式)
java·开发语言·python
mahuifa7 小时前
(7)python开发经验
python·qt·pyside6·开发经验