ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
# Python 输入和输出 在前面几个章节中,我们其实已经接触了 Python 的输入输出的功能。本章节我们将具体介绍 Python 的输入输出。 ## 输出格式美化 Python两种输出值的方式: 表达式语句 和 print() 函数。(第三种方式是使用文件对象的 write() 方法; 标准输出文件可以用 sys.stdout 引用。) 如果你希望输出的形式更加多样,可以使用 str.format() 函数来格式化输出值。 如果你希望将输出的值转成字符串,可以使用 repr() 或 str() 函数来实现。 str() 函数返回一个用户易读的表达形式。 repr() 产生一个解释器易读的表达形式。 ### 例如 ``` >>> s = 'Hello, world.' >>> str(s) 'Hello, world.' >>> repr(s) "'Hello, world.'" >>> str(1/7) '0.14285714285714285' >>> x = 10 * 3.25 >>> y = 200 * 200 >>> s = 'The value of x is ' + repr(x) + ', and y is ' + repr(y) + '...' >>> print(s) The value of x is 32.5, and y is 40000... >>> # The repr() of a string adds string quotes and backslashes: ... hello = 'hello, world\n' >>> hellos = repr(hello) >>> print(hellos) 'hello, world\n' >>> # The argument to repr() may be any Python object: ... repr((x, y, ('spam', 'eggs'))) "(32.5, 40000, ('spam', 'eggs'))" ``` 这里有两种方式输出一个平方与立方的表: ``` >>> for x in range(1, 11): ... print(repr(x).rjust(2), repr(x*x).rjust(3), end=' ') ... # Note use of 'end' on previous line 注意前一行 'end' 的使用 ... 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 >>> for x in range(1, 11): ... print('{0:2d} {1:3d} {2:4d}'.format(x, x*x, x*x*x)) ... 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 ``` **注意:**在第一个例子中, 每列间的空格由 print() 添加。 这个例子展示了字符串对象的 rjust() 方法, 它可以将字符串靠右, 并在左边填充空格。 还有类似的方法, 如 ljust() 和 center()。 这些方法并不会写任何东西, 它们仅仅返回新的字符串。 另一个方法 zfill(), 它会在数字的左边填充 0,如下所示: ``` >>> '12'.zfill(5) '00012' >>> '-3.14'.zfill(7) '-003.14' >>> '3.14159265359'.zfill(5) '3.14159265359' ``` str.format() 的基本使用如下: ``` >>> print('We are the {} who say "{}!"'.format('knights', 'Ni')) We are the knights who say "Ni!" ``` 括号及其里面的字符 (称作格式化字段) 将会被 format() 中的参数替换。 在括号中的数字用于指向传入对象在 format() 中的位置,如下所示: ``` >>> print('{0} and {1}'.format('spam', 'eggs')) spam and eggs >>> print('{1} and {0}'.format('spam', 'eggs')) eggs and spam ``` 如果在 format() 中使用了关键字参数, 那么它们的值会指向使用该名字的参数。 ``` >>> print('This {food} is {adjective}.'.format( ... food='spam', adjective='absolutely horrible')) This spam is absolutely horrible. ``` 位置及关键字参数可以任意的结合: ``` >>> print('The story of {0}, {1}, and {other}.'.format('Bill', 'Manfred', other='Georg')) The story of Bill, Manfred, and Georg. ``` '!a' (使用 ascii()), '!s' (使用 str()) 和 '!r' (使用 repr()) 可以用于在格式化某个值之前对其进行转化: ``` >>> 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. ``` 可选项 ':' 和格式标识符可以跟着字段名。 这就允许对值进行更好的格式化。 下面的例子将 Pi 保留到小数点后三位: ``` >>> import math >>> print('The value of PI is approximately {0:.3f}.'.format(math.pi)) The value of PI is approximately 3.142. ``` 在 ':' 后传入一个整数, 可以保证该域至少有这么多的宽度。 用于美化表格时很有用。 ``` >>> 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 ==> 4127 ``` 如果你有一个很长的格式化字符串, 而你不想将它们分开, 那么在格式化时通过变量名而非位置会是很好的事情。 最简单的就是传入一个字典, 然后使用方括号 '[]' 来访问键值 : ``` >>> 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: 8637678 ``` 也可以通过在 table 变量前使用 '**' 来实现相同的功能: ``` >>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678} >>> print('Jack: {Jack:d}; Sjoerd: {Sjoerd:d}; Dcab: {Dcab:d}'.format(**table)) Jack: 4098; Sjoerd: 4127; Dcab: 8637678 ``` ## 旧式字符串格式化 % 操作符也可以实现字符串格式化。 它将左边的参数作为类似 sprintf() 式的格式化字符串, 而将右边的代入, 然后返回格式化后的字符串. 例如: ``` >>> import math >>> print('The value of PI is approximately %5.3f.' % math.pi) The value of PI is approximately 3.142. ``` 因为 str.format() 比较新的函数, 大多数的 Python 代码仍然使用 % 操作符。但是因为这种旧式的格式化最终会从该语言中移除, 应该更多的使用 str.format(). ## 读和写文件 open() 将会返回一个 file 对象,基本语法格式如下: ``` open(filename, mode) ``` 实例: ``` >>> f = open('/tmp/workfile', 'w') ``` * 第一个参数为要打开的文件名。 * 第二个参数描述文件如何使用的字符。 mode 可以是 'r' 如果文件只读, 'w' 只用于写 (如果存在同名文件则将被删除), 和 'a' 用于追加文件内容; 所写的任何数据都会被自动增加到末尾. 'r+' 同时用于读写。 mode 参数是可选的; 'r' 将是默认值。 ## 文件对象的方法 本节中剩下的例子假设已经创建了一个称为 f 的文件对象。 ### f.read() 为了读取一个文件的内容,调用 f.read(size), 这将读取一定数目的数据, 然后作为字符串或字节对象返回。 size 是一个可选的数字类型的参数。 当 size 被忽略了或者为负, 那么该文件的所有内容都将被读取并且返回。 ``` >>> f.read() 'This is the entire file.\n' >>> f.read() '' ``` ### f.readline() f.readline() 会从文件中读取单独的一行。换行符为 '\n'。f.readline() 如果返回一个空字符串, 说明已经已经读取到最后一行。 ``` >>> f.readline() 'This is the first line of the file.\n' >>> f.readline() 'Second line of the file\n' >>> f.readline() '' ``` ### f.readlines() f.readlines() 将返回该文件中包含的所有行。 如果设置可选参数 sizehint, 则读取指定长度的字节, 并且将这些字节按行分割。 ``` >>> f.readlines() ['This is the first line of the file.\n', 'Second line of the file\n'] ``` 另一种方式是迭代一个文件对象然后读取每行: ``` >>> for line in f: ... print(line, end='') ... This is the first line of the file. Second line of the file ``` 这个方法很简单, 但是并没有提供一个很好的控制。 因为两者的处理机制不同, 最好不要混用。 ### f.write() f.write(string) 将 string 写入到文件中, 然后返回写入的字符数。 ``` >>> f.write('This is a test\n') 15 ``` 如果要写入一些不是字符串的东西, 那么将需要先进行转换: ``` >>> value = ('the answer', 42) >>> s = str(value) >>> f.write(s) 18 ``` ### f.tell() f.tell() 返回文件对象当前所处的位置, 它是从文件开头开始算起的字节数。 ### f.seek() 如果要改变文件当前的位置, 可以使用 f.seek(offset, from_what)函数, from_what 表示开始读取的位置,offset表示从from_what再移动一定量的距离,比如f.seek(10, 3)表示定位到第三个字符并再后移10个字符。 from_what值为0时表示文件的开始,它也可以省略,缺省是0即文件开头。下面给出一个完整的例子: ``` >>> f = open('/tmp/workfile', 'rb+') >>> f.write(b'0123456789abcdef') 16 >>> f.seek(5) # 移动到文件的第六个字节 5 >>> f.read(1) b'5' >>> f.seek(-3, 2) # 移动到文件的倒数第三字节 13 >>> f.read(1) b'd' ``` ### f.close() 在文本文件中 (那些打开文件的模式下没有 b 的), 只会相对于文件起始位置进行定位。 当你处理完一个文件后, 调用 f.close() 来关闭文件并释放系统的资源,如果尝试再调用该文件,则会抛出异常。 ``` >>> f.close() >>> f.read() Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: I/O operation on closed file <pre> <p> 当处理一个文件对象时, 使用 with 关键字是非常好的方式。在结束后, 它会帮你正确的关闭文件。 而且写起来也比 try - finally 语句块要简短:</p> <pre> >>> with open('/tmp/workfile', 'r') as f: ... read_data = f.read() >>> f.closed True ``` 文件对象还有其他方法, 如 isatty() 和 trucate(), 但这些通常比较少用。 ## pickle 模块 python的pickle模块实现了基本的数据序列和反序列化。 通过pickle模块的序列化操作我们能够将程序中运行的对象信息保存到文件中去,永久存储。 通过pickle模块的反序列化操作,我们能够从文件中创建上一次程序保存的对象。 基本接口: ``` pickle.dump(obj, file, [,protocol]) ``` 有了 pickle 这个对象, 就能对 file 以读取的形式打开: ``` x = pickle.load(file) ``` **注解:**从 file 中读取一个字符串,并将它重构为原来的python对象。 **file:** 类文件对象,有read()和readline()接口。 实例1: ``` #使用pickle模块将数据对象保存到文件 import pickle data1 = {'a': [1, 2.0, 3, 4+6j], 'b': ('string', u'Unicode string'), 'c': None} selfref_list = [1, 2, 3] selfref_list.append(selfref_list) output = open('data.pkl', 'wb') # Pickle dictionary using protocol 0. pickle.dump(data1, output) # Pickle the list using the highest protocol available. pickle.dump(selfref_list, output, -1) output.close() ``` 实例2: ``` #使用pickle模块从文件中重构python对象 import pprint, pickle pkl_file = open('data.pkl', 'rb') data1 = pickle.load(pkl_file) pprint.pprint(data1) data2 = pickle.load(pkl_file) pprint.pprint(data2) pkl_file.close() ```