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重构

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

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

相关推荐
天天被压力5 分钟前
【A股分钟级高频数据完全手册 #05】指数历史分钟数据接口:Pro版沪深指数分时回放
java·人工智能·python
ATMQuant25 分钟前
量化策略开发08:Lasso因子选择 - 因子动物园的断舍离
python·量化交易·lasso·vnpy·多因子选股
派大猩猩_1 小时前
Obsidian 笔记怎么同步多端:Nutstore Sync 大笔记库同步测评
笔记·obsidian·obsidian插件·obsidian同步
yeccheng2 小时前
MySQL 索引超详解:从底层 B+ 树到面试高频考点
java·开发语言·jvm·mysql·面试
联犀2 小时前
纯 Go 的 CAD 开源库 go-cad:直接解析 DWG/DXF,现在也能导出 PDF
开发语言·golang·pdf
ATMQuant3 小时前
量化指标解码21:逻辑回归支撑阻力 - 让逻辑回归给关键价位打分
python·机器学习·逻辑回归·量化交易·技术指标
zjxtxdy3 小时前
C++面向对象程序开发-类与对象
开发语言·c++
q0371803 小时前
深色 IDE 截图 OCR 老是漏字,我加了一层预处理,顺手把识别结果做成了可搜索历史
python
蜗牛8753 小时前
个人机票监控开源项目:6 个平台、10 条通道的自托管方案
python
秋名山小白4 小时前
Playwright UI 自动化测试实战教程
python·ui