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...

>>> # repr() 函数可以转义字符串中的特殊字符

... hello = 'hello, world '

>>> hellos = repr(hello)

>>> print(hellos)

'hello, world '

>>> # repr() 的参数可以是 Python 的任何对象

... 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=' ')

... # 注意前一行 '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. '

>>> f.read()

''

f.readline()

f.readline() 会从文件中读取单独的一行。换行符为 ' '。f.readline() 如果返回一个空字符串, 说明已经已经读取到最后一行。 >>> f.readline()

'This is the first line of the file. '

>>> f.readline()

'Second line of the file '

>>> f.readline()

''

f.readlines()

f.readlines() 将返回该文件中包含的所有行。

如果设置可选参数 sizehint, 则读取指定长度的字节, 并且将这些字节按行分割。 >>> f.readlines()

['This is the first line of the file. ', 'Second line of the file ']

另一种方式是迭代一个文件对象然后读取每行: >>> 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 ')

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 的值, 如果是 0 表示开头, 如果是 1 表示当前位置, 2 表示文件的结尾,例如: seek(x,0) : 从起始位置即文件首行首字符开始移动 x 个字符

seek(x,1) : 表示从当前位置往后移动x个字符

seek(-x,2):表示从文件的结尾往前移动x个字符

from_what 值为默认为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 "", line 1, in ?

ValueError: I/O operation on closed file

当处理一个文件对象时, 使用 with 关键字是非常好的方式。在结束后, 它会帮你正确的关闭文件。 而且写起来也比 try - finally 语句块要简短:

>>> 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()

python3中文手册-Python 输入和输出相关推荐

  1. python3 中文手册Python 入门指南

    Python 是一门简单易学且功能强大的编程语言.它拥有高效的高级数据结构,并且能够用简单而又高效的方式进行面向对象编程.Python 优雅的语法和动态类型,再结合它的解释性,使其在大多数平台的许多领 ...

  2. [转载] Python输入,输出,Python导入

    参考链接: Python输入,输出和导入 In this tutorial we will cover the basics of Python Input/Output and also how t ...

  3. Python输入,输出,Python导入

    In this tutorial we will cover the basics of Python Input/Output and also how to import a module in ...

  4. python输入和输出的区别_python2和python3的输入和输出区别介绍

    Python3 输入和输出 输出格式美化 Python两种输出值的方式: 表达式语句和 print() 函数. 第三种方式是使用文件对象的 write() 方法,标准输出文件可以用 sys.stdou ...

  5. python3中文手册-Python3.4中文手册 chm版

    Python3.4中文手册是为学python的朋友设计的一款免费的学习手册,内容丰富,包括使用python解释器.python简洁.深入python流程控制.数据结构.模块等内容,还有详细的实例,更容 ...

  6. python输入输出-python输入与输出

    python输出 python3中的输出 python3中的输出使用函数print(),示例如下: >>> print('hello kitty') print()也可接受多个参数, ...

  7. python输入姓名输出欢迎_python 3 基础之输入输出

    输出 格式化输出符号 格式符号转换 %c字符 %s字符串 %d有符号十进制整数 %u无符号十进制整数 %o八进制整数 %x十六进制整数(小写字母0x) %X十六进制整数(大写字母0X) %f浮点数 % ...

  8. 萌新学python(输入与输出)

    输出函数 print("Hello World") 编程中除了中文其他的请全部用英文输入法 上边的程序代表输出字符串Hello World 其中print()是输出函数,()里面的 ...

  9. python输入名字输出你好代码_003-输入和输出

    输入和输出 输出 1.输出字符串 用print加上字符串,就可以向屏幕上输出指定的文字.比如输出'hello, world',用代码实现如下:>>> print 'hello, wo ...

最新文章

  1. The Road to learn React书籍学习笔记(第三章)
  2. Shell 前后台任务
  3. 敲山震虎?继MongoDB之后,AWS又对Elasticsearch下手了
  4. 【C++ 语言】Visual Studio 配置 POSIX 线程 ( Windows 不支持 POSIX | 配置文件下载 | 库文件说明 | 配置过程 )
  5. Spring单例的线程安全性
  6. 教育孩子的一个很棒的方式
  7. The new NDK support in Android Studio 1.3
  8. android10适配机型华为,EMUI10适配机型进度公布 这10款机型将率先尝鲜
  9. p内的a截取字符串,会将a与p的其他内容撑开(IE6、IE7)
  10. 消息推送平台高可用实践(下)
  11. 在Web.Config中指定页面的基类
  12. 基于mysql的全文索引
  13. js书写原生ajax,javascript原生ajax写法
  14. pop3邮箱怎么设置收发服务器端口,常用邮箱的服务器(Smtp/POP3)地址和端口总结
  15. yum源及常用安装包整理
  16. Android发送图片到指定邮箱(仅客户端简单实现,不需服务端配合)
  17. Hutool XML 转JSON 后 parseArray踩坑
  18. 快手,字节跳动,百度,美团Offer之旅(Android面经分享)
  19. 17.代理_CDN_网络安全
  20. 数字时钟的设计与仿真

热门文章

  1. hdu 5909 Tree Cutting——点分治(树形DP转为序列DP)
  2. vue内引入jsPlumb流程控制器(一)
  3. BZOJ 1014 [JSOI2008]火星人prefix
  4. 【BZOJ4236】JOIOJI [DP]
  5. BZOJ 2434: [Noi2011]阿狸的打字机 [AC自动机 Fail树 树状数组 DFS序]
  6. 【CSS中width、height的默认值】
  7. 关于面向对象的总结和疑惑(转载可乐冰
  8. HashMap数据类型使用注意-不能使用基本数据类型
  9. AutoComplete选择之后,传PK触发动作
  10. python表白-520使用Python实现“我爱你”表白