Python小练习
来源:互联网 发布:java获得项目绝对路径 编辑:程序博客网 时间:2024/06/11 23:16
要求:
1. 把一个数字的list从小到大排序,然后写入文件,然后从文件中读取出来文件内容,然后反序,在追加到文件的下一行中
2. 分别把 string, list, tuple, dict写入到文件中
import codecsli = [12,44,65,90,232,49,130,81,329,7]li.sort() //将列表排序with codecs.open('D:\\file_test.txt','w') as file: for i in li: file.write(str(i) + '\n') //遍历列表之后,将其写入
with codecs.open('D:\\file_test.txt','r') as file1: value = file1.readlines() for lines in value: line = lines.strip('\n') //读取出内容,遍历之后去掉换行符号\n print line
st = 'hello python'tup = ('haha','xixi',88)dic = dict(a=1,b=2,c=3)li = ['dd',11,'yee']with codecs.open('D:\\file_test.txt','a') as file2: file2.write('*' * 30 + '\n') value.reverse() //反向排序 file2.writelines(value) file2.write('*' * 30 + '\n') file2.write(st + '\n') file2.writelines(str(tup) + '\n') file2.writelines(str(dic) + '\n') file2.writelines(str(li) + '\n')
运行结果:7124449658190130232329******************************3292321309081654944127******************************hello python('haha', 'xixi', 88){'a': 1, 'c': 3, 'b': 2}['dd', 11, 'yee']
阅读全文
0 0
- python小练习
- python小项目练习
- python小练习
- python练习小程序
- python小练习
- Python 小练习_battleship
- Python 小练习 阶乘
- Python-小练习
- Python小练习
- Python小练习一
- Python小练习1
- python练手小练习
- python小练习----秒表
- python第二次小练习
- Python小练习三
- Python小练习
- python小程序练习
- Python练习小案例
- Oracle 数据库 Record is locked by another user 问题解决办法
- Spring mvc HandlerMapping 实现机制
- CSS进阶
- BZOJ2460 线性基裸题
- SVG中stroke-dasharray及stroke-dashoffset属性
- Python小练习
- 心塞的电路实践
- -要成为精力的管理者,而不是时间的管理者
- Codeforces Round #443 (Div. 2)
- Spring mvc ViewResolver视图解析器实现机制
- 快速生成 markdown 表格:针对文件列表和文件大小
- js小球碰撞反弹
- 欢迎使用CSDN-markdown编辑器
- JAVA 23种设计模式