Python Tutorial 学习笔记5 --IO
2016-07-08 15:11
429 查看
There are two ways to format your output; the first way is to do all the string handling yourself; using string slicing and concatenation operations you can create any layout you can
imagine. The string types have some methods that perform useful operations for padding strings to a given column width; these will be discussed shortly. The second way is to use the
One question remains, of course: how do you convert values to strings? Luckily, Python has ways to convert any value to a string:
pass it to the
is meant to return representations of values which are fairly human-readable, while
meant to generate representations which can be read by the interpreter.
例1:
例2:the
of string objects, which right-justifies a string in a field of a given width by padding it with spaces on the left.There are similar methods
例3:There is another method,
which pads a numeric string on the left with zeros.
例4:The brackets and characters within them (called format fields) are replaced with the objects
passed into the
A number in the brackets refers to the position of the object passed into the
例5:
and
can be used to convert the value before it is formatted.
例6:Passing an integer after the
cause that field to be a minimum number of characters wide. This is useful for making tables pretty.
NOTES:This could also be done by passing the table as keyword arguments with the ‘**’ notation.
例7:Old string formatting. The
can also be used for string formatting.
例8:
a file object, and is most commonly used with two arguments:
例9:It is good practice to use the
when dealing with file objects.It is also much shorter than writing equivalent
例10:data exchange with json
例11:If f is a file object opened for writing, we can use dumps() serializes the object to a file.
imagine. The string types have some methods that perform useful operations for padding strings to a given column width; these will be discussed shortly. The second way is to use the
str.format()method.
One question remains, of course: how do you convert values to strings? Luckily, Python has ways to convert any value to a string:
pass it to the
repr()or
str()functions. The
str()function
is meant to return representations of values which are fairly human-readable, while
repr()is
meant to generate representations which can be read by the interpreter.
例1:
例2:the
str.rjust()method
of string objects, which right-justifies a string in a field of a given width by padding it with spaces on the left.There are similar methods
str.ljust()and
str.center().
例3:There is another method,
str.zfill(),
which pads a numeric string on the left with zeros.
例4:The brackets and characters within them (called format fields) are replaced with the objects
passed into the
str.format()method.
A number in the brackets refers to the position of the object passed into the
str.format()method.
例5:
'!s'(apply
str())
and
'!r'(apply
repr())
can be used to convert the value before it is formatted.
例6:Passing an integer after the
':'will
cause that field to be a minimum number of characters wide. This is useful for making tables pretty.
NOTES:This could also be done by passing the table as keyword arguments with the ‘**’ notation.
例7:Old string formatting. The
%operator
can also be used for string formatting.
例8:
open()returns
a file object, and is most commonly used with two arguments:
open(filename, mode).
例9:It is good practice to use the
withkeyword
when dealing with file objects.It is also much shorter than writing equivalent
try-
finallyblocks.
例10:data exchange with json
例11:If f is a file object opened for writing, we can use dumps() serializes the object to a file.
相关文章推荐
- Python动态类型的学习---引用的理解
- Python3写爬虫(四)多线程实现数据爬取
- 垃圾邮件过滤器 python简单实现
- 下载并遍历 names.txt 文件,输出长度最长的回文人名。
- install and upgrade scrapy
- Scrapy的架构介绍
- Centos6 编译安装Python
- 使用Python生成Excel格式的图片
- 让Python文件也可以当bat文件运行
- [Python]推算数独
- Python中zip()函数用法举例
- Python中map()函数浅析
- Python将excel导入到mysql中
- Python在CAM软件Genesis2000中的应用
- 使用Shiboken为C++和Qt库创建Python绑定
- FREEBASIC 编译可被python调用的dll函数示例
- Python 七步捉虫法