Python读书笔记第十一章:输入/输出
2014-02-28 10:35
232 查看
1.文件
程序与用户交互时,可以使用raw_input和print语句来完成这些功能。rjust可以得到一个按一定宽度右对齐的字符串。
通过创建一个file类的对象来打开一个文件,分别使用read、readline或write方法来读写文件。对文件的读写能力依赖于你在打开文件时指定的模式,最后通过调用close方法来告诉Python完成对文件的使用。
#!/usr/bin/python
# Filename: using_file.py
poem = '''\
Programming is fun
When the work is done
if you wanna make your work also fun:
use Python!
'''
f = file('poem.txt', 'w') # open for 'w'riting
f.write(poem) # write text to file
f.close() # close the file
f = file('poem.txt')
# if no mode is specified, 'r'ead mode is assumed by default
while True:
line = f.readline()
if len(line) == 0: # Zero length indicates EOF
break
print line,
# Notice comma to avoid automatic newline added by Python
f.close() # close the file
输出:
$ python using_file.py
Programming is fun
When the work is done
if you wanna make your work also fun:
use Python!
模式可以为‘r’(read), 'w'(write), 'a'(append),此外还有很多的模式可以使用。
程序首先用w模式打开文件,然后使用write方法来写文件,最后使用close关闭这个文件。
如果没有指定模式,读模式会作为默认模式。在一个循环中,使用readline方法来读每一行。这个方法返回包括行末换行符的一个完整行。因为从文件读到的内容已经以换行符结尾,所以我们在print语句上使用都好来消除自动换行。
2.存储器
Python的pickle模块可以让你在一个文件中储存任何Python对象,之后你又可以完整无缺地取出来。这被成为持久地储存对象。与Pickle相同的还有一个cPickle模块(用C语言编写,速度快1000倍)。
#!/usr/bin/python
# Filename: pickling.py
import cPickle as p
#import pickle as p
shoplistfile = 'shoplist.data'
# the name of the file where we will store the object
shoplist = ['apple', 'mango', 'carrot']
# Write to the file
f = file(shoplistfile, 'w')
p.dump(shoplist, f) # dump the object to a file
f.close()
del shoplist # remove the shoplist
# Read back from the storage
f = file(shoplistfile)
storedlist = p.load(f)
print storedlist
输出:
$ python pickling.py
['apple', 'mango', 'carrot']
使用import...as语法以便于我们可以使用更短的模块名称。为了在文件里储存一个对象,调用储存器模块的dump函数,把对象储存到打开的文件中。这个过程称为储存。然后使用load函数返回来取会对象,这个过程称为取储存。
程序与用户交互时,可以使用raw_input和print语句来完成这些功能。rjust可以得到一个按一定宽度右对齐的字符串。
通过创建一个file类的对象来打开一个文件,分别使用read、readline或write方法来读写文件。对文件的读写能力依赖于你在打开文件时指定的模式,最后通过调用close方法来告诉Python完成对文件的使用。
#!/usr/bin/python
# Filename: using_file.py
poem = '''\
Programming is fun
When the work is done
if you wanna make your work also fun:
use Python!
'''
f = file('poem.txt', 'w') # open for 'w'riting
f.write(poem) # write text to file
f.close() # close the file
f = file('poem.txt')
# if no mode is specified, 'r'ead mode is assumed by default
while True:
line = f.readline()
if len(line) == 0: # Zero length indicates EOF
break
print line,
# Notice comma to avoid automatic newline added by Python
f.close() # close the file
输出:
$ python using_file.py
Programming is fun
When the work is done
if you wanna make your work also fun:
use Python!
模式可以为‘r’(read), 'w'(write), 'a'(append),此外还有很多的模式可以使用。
程序首先用w模式打开文件,然后使用write方法来写文件,最后使用close关闭这个文件。
如果没有指定模式,读模式会作为默认模式。在一个循环中,使用readline方法来读每一行。这个方法返回包括行末换行符的一个完整行。因为从文件读到的内容已经以换行符结尾,所以我们在print语句上使用都好来消除自动换行。
2.存储器
Python的pickle模块可以让你在一个文件中储存任何Python对象,之后你又可以完整无缺地取出来。这被成为持久地储存对象。与Pickle相同的还有一个cPickle模块(用C语言编写,速度快1000倍)。
#!/usr/bin/python
# Filename: pickling.py
import cPickle as p
#import pickle as p
shoplistfile = 'shoplist.data'
# the name of the file where we will store the object
shoplist = ['apple', 'mango', 'carrot']
# Write to the file
f = file(shoplistfile, 'w')
p.dump(shoplist, f) # dump the object to a file
f.close()
del shoplist # remove the shoplist
# Read back from the storage
f = file(shoplistfile)
storedlist = p.load(f)
print storedlist
输出:
$ python pickling.py
['apple', 'mango', 'carrot']
使用import...as语法以便于我们可以使用更短的模块名称。为了在文件里储存一个对象,调用储存器模块的dump函数,把对象储存到打开的文件中。这个过程称为储存。然后使用load函数返回来取会对象,这个过程称为取储存。
相关文章推荐
- 第十一章:流类库与输入/输出
- 第十一章 字符串和字符串函数(字符串标准输入输出库函数)总结 140525
- 第十一章 字符串和字符串函数(字符串标准输入输出库函数)总结 140527
- C语言之路 第十一章 控制台输入/输出
- 对于输入的任意一个非负十进制整数,打印输出与其等值的八进制数
- 小白成长日记(13)--(c++)文件的输入输出
- 第17 题:题目:在一个字符串中找到第一个只出现一次的字符。如输入abaccdeff,则输出b。
- 华为机试题:输入一组大于等于0的整数,从小到大排序输出,输出有连续数时,只输出连续数的最小和最大值
- 编写程序,输出用户输入的两个数中的较大者
- 输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。
- Java输入输出及高精度
- 此小程序用于输入两个坐标,输出他们之间的距离
- Java输入输出(IO)和流的基本概念
- 输入行数(奇数),输出对应菱形
- 【引用类型做参数】第17周项目1--体会函数参数传递(输入三个整数,将其按从大到小的顺序输出)
- 格式化输入与输出
- MR案例:输出/输入SequenceFile
- 任意输入一个三位数,判断该书是否是水仙花数。若是输出“yes”,否则输出“no”。
- 汇编语言:从键盘输入十进制数,输出该十进制数的十进制,二进制(B),十六进制(H),八进制(Q)表示
- 字节输入 输出流