python 练习
来源:互联网 发布:深圳市思普达软件系统 编辑:程序博客网 时间:2024/06/11 01:31
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time : 2017/10/30 0030 11:58
# @File : David.py
#正向排序
import codecs
l = [1,5,3,8,2,4]
l.sort()
#写入文件
with codecs.open('1.txt','wb') as fd:
for i in l:
fd.write(str(i))
fd.write('\n')
#读取文件并还原成列表
l1=[]
with codecs.open('1.txt','rb') as fd:
for i in fd.read():
l1.append(i)
#反向排序
l1.reverse()
#删除空格
l1.remove('\n')
#追加写入列表
with codecs.open('1.txt','ab') as fd:
for i in l1:
fd.write(str(i))
#追加字符串
str1 = 'abc'
with codecs.open('1.txt','ab') as fd:
fd.write('\n')
fd.write(str1)
#追加列表
li = [1,2,3]
with codecs.open('1.txt', 'ab') as fd:
fd.write('\n')
for i in li:
fd.write(str(i))
#追加元组
tu = ('one','two','three')
with codecs.open('1.txt', 'ab') as fd:
fd.write('\n')
for i in tu:
fd.write(i)
#追加字典
dic = {1:'a',2:'b'}
with codecs.open('1.txt', 'ab') as fd:
for line in dic.iteritems():
fd.write('\n')
for i in line:
fd.write(str(i))
# -*- coding:utf-8 -*-
# @Time : 2017/10/30 0030 11:58
# @File : David.py
#正向排序
import codecs
l = [1,5,3,8,2,4]
l.sort()
#写入文件
with codecs.open('1.txt','wb') as fd:
for i in l:
fd.write(str(i))
fd.write('\n')
#读取文件并还原成列表
l1=[]
with codecs.open('1.txt','rb') as fd:
for i in fd.read():
l1.append(i)
#反向排序
l1.reverse()
#删除空格
l1.remove('\n')
#追加写入列表
with codecs.open('1.txt','ab') as fd:
for i in l1:
fd.write(str(i))
#追加字符串
str1 = 'abc'
with codecs.open('1.txt','ab') as fd:
fd.write('\n')
fd.write(str1)
#追加列表
li = [1,2,3]
with codecs.open('1.txt', 'ab') as fd:
fd.write('\n')
for i in li:
fd.write(str(i))
#追加元组
tu = ('one','two','three')
with codecs.open('1.txt', 'ab') as fd:
fd.write('\n')
for i in tu:
fd.write(i)
#追加字典
dic = {1:'a',2:'b'}
with codecs.open('1.txt', 'ab') as fd:
for line in dic.iteritems():
fd.write('\n')
for i in line:
fd.write(str(i))
阅读全文
0 0
- python练习
- python练习
- Python练习
- python练习
- python练习
- #python#练习
- python 练习
- 【python练习】
- python练习
- python练习
- python练习
- python练习
- Python练习
- python练习
- Python练习
- python 练习
- python练习
- python 练习
- 类的成员之三:构造器(构造方法)
- TCP UDP 通信机制和优劣
- sqlite安装教程
- 简单爬虫----爬取斗罗大陆3的100个章节(实现较复杂的翻页)
- ubuntu部署vsftpd
- python 练习
- springmvc注解之@SessionAttribute和@ModelAttribute
- 寻路插件A* Pathfinding Project_Pro的系统学习(三)_AI Path组件_Seeker组件_RaycastSimplifier后期处理组件_移动路径进行修正功能
- 项目中easyUI的知识点
- 条件运算符的应用
- HashSet,TreeSet和LinkedHashSet的区别
- 在unbuntu环境环境中搭建mingw-w64,编译出a.exe到windows环境的执行文件
- 使用Servlet3.0新特性进行web开发小demo
- 29.Oracle杂记——Oracle常用动态视图v$rowcache