python 教程 第十章、 输入/输出
来源:互联网 发布:在线网络个人理财公司 编辑:程序博客网 时间:2024/06/14 22:10
第十章、 输入/输出
1) 文件
poem = '''Programming is fun use Python!'''
f = file('poem.txt', 'w') # open for 'w'riting
f.write(poem) # write text to file
f.close() # close the file
可以使用help(file)来了解详情。
2) 储存器
pickle在文件中储存Python对象,cPickle(C语言,更快)
import cPickle as p
shoplistfile = 'shoplist.data'
shoplist = ['apple', 'mango', 'carrot', 'donggua']
f = file(shoplistfile, 'w')
p.dump(shoplist, f) # dump the object to a file
f.close()
#使用pickle
import pickle
object = someClass()
file = open(filename, 'wb') # Create external file
pickle.dump(object, file) # Save object in file
import pickle
file = open(filename, 'rb')
object = pickle.load(file) # Fetch it back later
#使用shelve
import shelve
object = someClass()
dbase = shelve.open('filename')
dbase['key'] = object # Save under key
import shelve
dbase = shelve.open('filename')
object = dbase['key'] # Fetch it back later
- python 教程 第十章、 输入/输出
- python 教程 第十章、 输入/输出
- 《简明Python教程》之输入与输出
- Python教程之六-----输入和输出
- python简明教程-第十章解决问题-备份文件
- 廖雪峰python教程阅读之输入和输出
- [疯狂Java讲义精粹] 第十章|输入/输出
- Python读书笔记第十一章:输入/输出
- python 输入、输出、文件
- python 输入和输出
- python输入和输出
- Python输入和输出
- python 输入与输出
- Python 输入和输出
- Python 输入与输出
- Python输入和输出
- 【Python】 Python输入和输出
- python的输入和输出
- python 教程 第五章、 函数
- python 教程 第六章、 模块
- python 教程 第七章、 数据结构
- python 教程 第八章、 第一个python程序
- python 教程 第九章、 类与面向对象
- python 教程 第十章、 输入/输出
- python 教程 第十一章、 异常
- python 教程 第十二章、 标准库
- python 教程 第十三章、 特殊的方法
- SecureCRT自动登录Linux并设置时间
- SecureCRT循环检查设备状态
- SecureCRT下载/注册/安装镜像文件
- python 教程 第十四章、 地址薄作业
- POJ 2709 Painter