python 教程 第十章、 输入/输出
第十章、 输入/输出
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基本语法_输入/输出语句详解
目录 目录 前言 输入 raw_input input raw_input 和 input 的区别 输出 print print 基本格式化输出 print复杂格式化输出 flags标志位 width ...
- python 教程 第二十章、 数据库编程
第二十章. 数据库编程 环境设置 1).安装MySQL-python http://www.lfd.uci.edu/~gohlke/pythonlibs/ MySQL-python-1.2.3.win ...
- A Byte of Python 笔记(10)输入/输出:文件和储存器
第12章 输入/输出 大多数情况下,我们需要程序与用户交互.从用户得到输入,然后打印一些结果. 可以分别使用 raw_input 和 print 语句来完成这些功能.对于输出,可以使用多种多样的 s ...
- Python基础学习笔记---5.输入\输出 I\O文件操作目录
在很多时候,你会想要让你的程序与用户(可能是你自己)交互.你会从用户那里得到输入,然后打印一些结果.我们可以分别使用 raw_input 和 print 语句来完成这些功能.对于输出,你也可以使用多种 ...
- Python教程(2.1)——控制台输入
这一节,我们来学习如何写一个简单的Python程序. 我们知道,很多编程语言一开始就是学习怎么输出"Hello, world",对吧?那么,现在我们来学习怎么用Python输出&q ...
- 基于 Python 和 Pandas 的数据分析(3) --- 输入/输出 基础
这一节, 我们要讨论 Pandas 的输入与输出, 并且应用在现实的实际例子中. 为了得到大量的数据, 向大家推荐一个网站 Quandl. Quandl 有很多免费和付费的资源. 这个网站最大的优势在 ...
- Python基础二(输入与输出)
通常,一个程序都会有输入/输出,这样可以与用户进行交互.用户输入一些信息,你会对他输入的内容进行一些适当的操作,然后再输出给用户想要的结果.Python的输入/输出,我们可以用input进行输入,pr ...
- python 变量,输入,输出
目录 2.0 注释 2.1 变量 2.2 变量名命名规范 2.3 常量 2.4 输入 input 2.5 输出 print 2.6 关于开发工具 2.0 注释 python的注释方法 "&q ...
- 2. Python中的基本输入、输出、格式化
本文利用的是Python 3.x版本,建议学习3.x版本 Python中的基本输入.输出.格式化 1. 输入 使用input([prompt])读取一行,将其转换为string类型并返回,input的 ...
随机推荐
- Spring中@Async用法详解及简单实例
Spring中@Async用法 引言: 在Java应用中,绝大多数情况下都是通过同步的方式来实现交互处理的:但是在处理与第三方系统交互的时候,容易造成响应迟缓的情况,之前大部分都是使用多线程来完成此类 ...
- POJ 3132 & ZOJ 2822 Sum of Different Primes(dp)
题目链接: POJ:id=3132">http://poj.org/problem?id=3132 ZOJ:http://acm.zju.edu.cn/onlinejudge/show ...
- [AngularJS NG-redux] Integrate Redux Devtools
In this lesson, we are going to learn how to integrate Redux Devtools into our Angular application. ...
- swift学习第九天:可选类型以及应用场景
可选类型的介绍 注意: 可选类型时swift中较理解的一个知识点 暂时先了解,多利用Xcode的提示来使用 随着学习的深入,慢慢理解其中的原理和好处 概念: 在OC开发中,如果一个变量暂停不使用,可以 ...
- Java提高:采用异常链传递异常
一.分析 异常需要封装,但是仅仅封装还是不够的,还需要传递异常.一个系统的友好型的标识,友好的界面功能是一方面,另一方面就是系统出现非预期的情况的处理方式了. 二.场景 比如我们的JEE项目一般都又三 ...
- [Recompose] Add Local State with Redux-like Reducers using Recompose
Learn how to use the 'withReducer' higher order component using the alternative reducer form. If you ...
- [Angular] FormBuildAPI
Using FormBuilder API can simply our code, for example we want to refactor following code by using F ...
- spring+aspectJ的实现
AspectJ:(Java社区里最完整最流行的AOP框架) spring自身也有一套AOP框架,但相比较于AspectJ,更推荐AspectJ 在Spring2.0以上版本中,可以使用基于Aspect ...
- [Ramda] Get Deeply Nested Properties Safely with Ramda's path and pathOr Functions
In this lesson we'll see how Ramda's path and pathOr functions can be used to safely access a deeply ...
- UItableview正在滚动的时候进行操作容易出问题
tableview正在滚动的时候进行其它点击事件操作容易出问题,有时候会出现莫名其妙的数组越界的bug, 解决方法:1.对objectatindex方法进行异常判断 2. [_tableview se ...