python学习-Day11-12-复习
来源:互联网 发布:毕向东java笔记 编辑:程序博客网 时间:2024/06/03 21:40
把一个数字的list从小到大排序,然后写入文件,然后从文件中读取出来文件内容,然后反序,在追加到文件的下一行
list = [4,22,445,678,43,678,43,45,92 ]
list1 = sorted(list)
f = open('2.txt','a')
f.writelines(str(list1))
f.writelines('\n')
f.close()
fd = open('2.txt')
a2 = fd.readlines()
print (a2)
fd.close()
['[2, 5, 6, 6, 7, 7, 8, 32, 43, 54, 78, 89, 453, 576]\n', '[576, 453, 89, 78, 54, 43, 32, 8, 7, 7, 6, 6, 5, 2]\n']
分别把 string, list, tuple, dict写入到文件中
#!/usr/bin/env python
# -*- coding:utf-8 -*
string
=
'this si a string'
list1
=
[
'hello'
,
'word'
,
'you'
]
tuple1
=
(
'hello'
,
'word'
,
'you'
)
dict1
=
{
'a'
:
1
,
'b'
:
2
,
'c'
:
3
}
with
open
(
'3.txt'
,
'w'
) as fd:
fd.write(string
+
'\n'
)
fd.write(
str
(list1)
+
'\n'
)
fd.write(
str
(tuple1)
+
'\n'
)
fd.write(
str
(dict1)
+
'\n'
)
with
open
(
'3.txt'
,
'r'
) as fd:
a
=
fd.read()
print
(a)
输出结果
this si a string
[
'hello'
,
'word'
,
'you'
]
(
'hello'
,
'word'
,
'you'
)
{
'a'
:
1
,
'b'
:
2
,
'c'
:
3
}
阅读全文
0 0
- python学习-Day11-12-复习
- python学习日志--day11
- python学习之路-RabbitMQ-day11
- Python-day11
- Python的学习笔记DAY11---python和Mysql
- 【javase复习】## day11 其他流 ##
- day11.12
- python day11(20170306 )
- Python--day11 函数2
- java基础学习day11
- C++ 学习笔记day11
- java学习day11
- java学习day11
- Android学习笔记day11
- java学习day11
- Linux学习-day11
- iOS开发-Day11-C的复习
- 【javase复习】## day11 其他流----打印流 ##
- Arrays.asList()踩过的坑
- 电脑操作:把文件上传到kindle,kindle上传文件 ,upload文件到kindle
- 图像处理中的距离相关概念
- PAT (Basic Level) Practise (中文)1012. 数字分类 (20)
- java-第六天
- python学习-Day11-12-复习
- 美团云召开首届人工智能峰会 生态成关键词
- AngularJS中的谷歌地图开发
- [zz系列]c语言的随机数
- 建设成为世界第五大经济体 破解阿里云生态密码
- [CocosCreator]扑克翻牌效果
- Fragment切换
- iOS面试:如何在平时工作中积累经验
- [微信小程序]计算自己手机到指定位置的距离