Python txt文件如何转换成字典
作者:15号计划 时间:2022-01-10 21:55:05
处理前文件内容
代码
处理后的
# 读取代码
fr = open('three.txt', 'r')
dic = {}
keys = [] # 用来存储读取的顺序
for line in fr:
v = line.strip().split(':')
dic[v[0]] = v[1]
keys.append(v[0])
fr.close()
print(dic)
# # 写入文件代码 通过keys的顺序写入
# fw = open('wdic.txt', 'w')
# for k in keys:
# fw.write(k + ':' + dic[k] + '\n')
# fw.close()
结果显示
来源:https://www.cnblogs.com/san357/p/13882715.html
标签:Python,txt,文件,字典
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
在Django中URL正则表达式匹配的方法
2021-06-09 22:38:57
![](https://img.aspxhome.com/file/2023/2/94012_0s.jpg)
python基于openpyxl生成excel文件
2022-08-03 03:10:47
![](https://img.aspxhome.com/file/2023/4/82684_0s.png)
一位网友的DIV CSS编码笔记——XieBiji
2008-09-12 12:22:00
介绍讲解MySQL安装下载登录
2010-10-25 20:34:00
oracle快速删除重复的记录
2010-07-23 13:03:00
JavaScript实现淘宝网图片的局部放大功能
2023-08-25 05:44:56
![](https://img.aspxhome.com/file/2023/0/56070_0s.jpg)
js 采用delete实现继承示例代码
2023-07-17 09:06:52
Linux环境MySQL服务器级优化讲解
2008-12-04 17:21:00
asp使用XMLHTTP下载远程数据输出到浏览器
2007-11-04 10:34:00
python实现字符串和字典的转换
2023-03-02 02:57:18
眼未动,心已动【碳酸饮料会】
2009-09-01 19:32:00
![](https://img.aspxhome.com/file/UploadPic/20099/1/t1qlbxxobrxxxxxxxx-455-221-73s.jpg)
python 如何在list中找Topk的数值和索引
2022-01-20 10:28:27
关于长度单位pt、px、dpi的误解
2008-06-01 13:30:00
Django框架基础模板标签与filter使用方法详解
2022-10-25 18:14:43
![](https://img.aspxhome.com/file/2023/5/66295_0s.png)
python使用RNN实现文本分类
2023-10-17 14:07:18
![](https://img.aspxhome.com/file/2023/1/96461_0s.jpg)
如何利用python写GUI及生成.exe可执行文件
2023-06-26 00:42:57
![](https://img.aspxhome.com/file/2023/4/59824_0s.png)
python查看数据类型的方法
2021-11-12 03:42:17
![](https://img.aspxhome.com/file/2023/5/84035_0s.png)
SQLServer中的切割字符串SplitString函数
2011-12-01 08:14:09
django与小程序实现登录验证功能的示例代码
2023-08-04 01:06:58
anaconda jupyter不能导入安装的lightgbm解决方案
2021-09-15 19:24:45
![](https://img.aspxhome.com/file/2023/5/83855_0s.jpg)