文件的输出输入与数据的存储反存储:
存储模块:pickle 和cPickle (C语言编写,速度远远快于前者),用法与file.write()差不多,引用
example:import cPickle as p
p.dump(data,file) #file='file.data'
p.load(file)
file.close()
一般有两种输出方法: expression statements 和 print .第三种write()用于文件对象,标准的输出文件可引用sys.stdout.
print 用法:
print x
等价于
import sys
sys.stdout.write(str(x)+'\n')
如果这样写:
import sys
sys.stdout=open(file)
print x,y
这样我们可以将x,y直接输出到文件中,这种方法也叫重定向输出。
再举一种重定向,不影响后面的print 原始输出流
f=open(file,'a')
print >>f,x,y
x,y输出到文件对象f中,这个表达式只是暂时的重定向输出,后面的print 语句依然是原始输出。
排版输出两种:1、通过对字符串的各种操作进行输出。参考string
2、用str.format()
str()
str():返回一个易读的数据表达式。
repr():产生一个解释器能读的表达式。
用于数字,列表和字典时,两个函数返回的结果是一样的,
for x in range(1, 11):
... print repr(x).rjust(2), repr(x*x).rjust(3),
... # Note trailing comma on previous line
... print repr(x*x*x).rjust(4)
...
1 1 1
2 4 8
3 9 27
4 16 64
5 25 125
6 36 216
7 49 343
8 64 512
9 81 729
10 100 1000
zfill(),rjust(),ljust(),center()
>>> '12'.zfill(5) '00012' >>> '-3.14'.zfill(7) '-003.14' >>> '3.14159265359'.zfill(5) '3.14159265359'
>>> print 'We are the {} who say "{}!"'.format('knights', 'Ni') We are the knights who say "Ni!">>> print '{0} and {1}'.format('spam', 'eggs') #后面表示要显示的数组 spam and eggs >>> print '{1} and {0}'.format('spam', 'eggs') eggs and spamIf keyword arguments are used in the format() method, their values are referred to by using the name of the argument.
>>> print 'This {food} is {adjective}.'.format( ... food='spam', adjective='absolutely horrible') This spam is absolutely horrible.Positional and keyword arguments can be arbitrarily combined:
>>> print 'The story of {0}, {1}, and {other}.'.format('Bill', 'Manfred',other='Georg') #前一个otherThe story of Bill, Manfred, and Georg.'!s' (apply str()) and '!r' (apply repr()) can be used to convert the value before it is formatted.
>>> import math >>> print 'The value of PI is approximately {}.'.format(math.pi) The value of PI is approximately 3.14159265359. >>> print 'The value of PI is approximately {!r}.'.format(math.pi) The value of PI is approximately 3.141592653589793.An optional ':' and format specifier can follow the field name. This allows greater control over how the value is formatted. The following example truncates Pi to three places after the decimal.
>>> import math >>> print 'The value of PI is approximately {0:.3f}.'.format(math.pi) The value of PI is approximately 3.142.Passing an integer after the ':' will cause that field to be a minimum number of characters wide. This is useful for making tables pretty.
>>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 7678} >>> for name, phone in table.items(): ... print '{0:10} ==> {1:10d}'.format(name, phone) ... Jack ==> 4098 Dcab ==> 7678 Sjoerd ==> 4127If you have a really long format string that you don’t want to split up, it would be nice if you could reference the variables to be formatted by name instead of by position. This can be done by simply passing the dict and using square brackets '[]' to access the keys
>>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678} >>> print ('Jack: {0[Jack]:d}; Sjoerd: {0[Sjoerd]:d}; ' ... 'Dcab: {0[Dcab]:d}'.format(table)) Jack: 4098; Sjoerd: 4127; Dcab: 8637678This could also be done by passing the table as keyword arguments with the ‘**’ notation.
>>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678} >>> print 'Jack: {Jack:d}; Sjoerd: {Sjoerd:d}; Dcab: {Dcab:d}'.format(**table) Jack: 4098; Sjoerd: 4127; Dcab: 8637678This is particularly useful in combination with the new built-in vars() function, which returns a dictionary containing all local variables.
For a complete overview of string formatting with str.format(), see Format String Syntax.
%号:
>>> import math >>> print 'The value of PI is approximately %5.3f.' % math.pi The value of PI is approximately 3.142.read and write file:) >>> print f <open file '/tmp/workfile', mode 'w' at 80a0960>