首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > perl python >

「学习札记——Python」Python输入和输出

2013-03-04 
「学习笔记——Python」Python输入和输出7 Python 输入和输出呈现程序输出结果的方式有很多,可以以可读方式打

「学习笔记——Python」Python输入和输出

7 Python 输入和输出

呈现程序输出结果的方式有很多,可以以可读方式打印出来,也可以写入文件以便将来使用。这一章,将会讲述这些可能的方式。

Table of Contents1 输入格式2 读写文件

很多时候,我们不仅仅想只打印出结果,还对输出格式有所需求。有两种方式可以完成这一点,一是使用字符串的分割,合并,等 功能自己确定输出格式,你可以得到你想要的任何布局。二是使用 str.format() 函数。 这里有一个问题,如何把各种各样的值转化为字符串呢?Python 提供了 repr() 和 str() 来将任何值转化为字符串。 str() 函数会返回人类易读的格式, 而repr()则会返回供解释器读取的格式。但是,如果转化不成人类易读的方式,两个函数 的输出就会一样。

>>> print str('hello\n')hello>>> print repr('hello\n')'hello\n'

下面是两种打印列表的方式:

>>> for i in range(1,11):...     print repr(i).rjust(2), repr(i*i).rjust(3),...     print repr(i*i*i).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  72910 100 1000
>>> for i in range(1,11):...     print '{0:2d} {1:3d} {2:4d}'.format(i,i*i,i*i*i)...  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  72910 100 1000

注意2,3,4是指占几列,列之间的空格是python自动加上的,除str.rjust()之外,还有str.ljust(),str.center(), 它们默认不会截断,除非你用 r.ljust(n)[:n] 指定. : 前的数字表示这一列的内容显示format后的第几个参数,后面的数字表示占几列

还有一个方法,str.zfill(),可以在数字左边加0.

>>> '12'.zfill(4)'0012'

str.format() 使用的基本方式就是

>>> 'this is a {} called {}'.format('language','python')'this is a language called python'

{}内的内容决定了这个位置是什么

>>> 'this is a {} called {}'.format('language','python')'this is a language called python'>>> 'this is a {0} called {1}'.format('language','python')'this is a language called python'>>> 'this is a {1} called {0}'.format('language','python')'this is a python called language'>>> 'this is a {1} called {1}'.format('language','python')'this is a python called python'

如果{}内出现了关键字,其内容由format后的定义决定

>>> 'my favourite language is {lan}'.format(lan = 'python')'my favourite language is python'

旧的字符串格式 str.format() 是一种新的字符串格式化方式,以前的代码都使用 %, 和sprintf差不多

>>> pi = 3.1415926>>> 'test float format:%5.2f' % pi'test float format: 3.14'

open() 返回一个文件对象,经常带有两个参数:open(filename, mode) mode 可以为:

r: 只读w: 只写a: 添加r+: 读写mode 也可以缺省,默认为r
>>> open('fileToTest.hi', 'r')<open file 'fileToTest.hi', mode 'r' at 0xb6e6ad30>

上述代码会返回一个文件对象,我们可以用它进行文件操作

>>> fileobj = open('fileToTest.hi', 'r')>>> fileobj.read()'this is a file to be test by python\nthis is second line\nthis is end line'>>> fileobj.read()''

把文件内容读完后,再去读就会返回空字符串

还可以一行一行读

>>> fileobj = open('fileToTest.hi', 'r')>>> fileobj.readline()'this is a file to be test by python\n'>>> fileobj.readline()'this is second line\n'>>> fileobj.readline()'this is end line'

或者返回多行构成的list

>>> fileobj = open('fileToTest.hi', 'r')>>> fileobj.readlines()['this is a file to be test by python\n', 'this is second line\n', 'this is end line']

另一种逐行读取的方式是:

>>> fileobj = open('fileToTest.hi', 'r')>>> for line in fileobj:...     print line... this is a file to be test by pythonthis is second linethis is end line

可以使用 f.wirte(string) 写入文件

>>> fw = open('fileToTest.hi', 'w')>>> fw.write('Hello,pyhon\nhi,jack')>>> fw.close()

写入之后要用f.close()关闭文件对象,否则f.write(str)没有真正写入文件。

如果一个值不是字符串,可以用str()先转化为字符串,再用f.wirte()写入。

f.tell()返回文件对象当前位置,f.seek(offset, fromWhat) 用于设置当前位置

>>> fr = open('fileToTest.hi', 'r')>>> fr.tell()0L>>> fr.read()'hihio,pyhon\nhi,jack'>>> fr.tell()19L>>> fr.read()''>>> fr.seek(0,0)>>> fr.read()'hihio,pyhon\nhi,jack'

如果仅仅是读写字符串,那么没什么大问题,但是如果文件中存储的是整数,或者更复杂的数据结构,比如字典。就会 遇到麻烦,因为读写都是针对字符串而言的。鉴于这个原因,python提供了名为 pickle 的模块。可以将任何类型的 python 对象转化为字符串,这个过程称为 picking. 反过来也可以将字符串转化为任何类型的对象,这个过程称为 unpicking.

例如:x 是一个对象, f 是一个用于写的文件对象, picking过程就是:

pickle.dump(x, f)

unpicking过程:

x = pickle.load(f)

下面做一个测验

>>> fw = open('foo.c', 'w')>>> x = {'asdf','qwert'}>>> import pickle>>> pickle.dump(x,fw)>>> fw.close()>>> fr = open('foo.c', 'r')>>> y = pickle.load(fr)>>> yset(['qwert', 'asdf'])

原文链接:http://docs.python.org/2/tutorial/inputoutput.html

热点排行