Python输入输出详解
来源:互联网 发布:淘宝主图视频拍摄技巧 编辑:程序博客网 时间:2024/06/01 07:33
http://blog.csdn.net/pipisorry/article/details/24143801
Python基本输入输出教程
python内置输入函数
- input(...)
- input([prompt]) -> string
- Read a string from standard input. The trailing newline is stripped.
- If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.
- On Unix, GNU readline is used if enabled. The prompt string, if given,
- is printed without a trailing newline before reading.
- >>>
读取一个字符串,注意换行符是被剥离的。input可以有一个参数,用来提示输入信息的,下面具体
例子:
- >>> name = input("your name is:")
- your name is:kiritor
- >>> print(name)
- kiritor
- >>>
- If the prompt argument is present, it is written to standard output without a trailing newline.
- The function then reads a line from input, converts it to a string
python使用字符串构建stdin对象
[操作系统服务:其它模块: io模块]
皮皮Blogpython内置输出函数
1. print()函数
Python2.7中是有print语句和内置print函数的,而在Python3.3中,已经没有print语句了,只有print函数,而其实以前的print语句的功能就是print函数默认形式的功能,所以我们在这里就只看看Python3.3中的内置函数print()。函数原型
print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
flush=False是Python3.3加上去的参数。
objects中每一个对象都会被转化为string的形式,然后写到file指定的文件中,默认是标准输出(sys.stdout),每一个对象之间用sep分隔,默认是空格;所有对象都写到文件后,会写入end,默认是换行。
sep和end参数
1 from __future__ import print_function #Python2.7中使用print()函数,Python3.2中这行代码就不需要了2 d = {1:'a', 2:'b'}3 t = (4, 5, 6)4 l = ['love', 'happiness']5 print(d, t, l, sep='~', end='^_^\n'){1: 'a', 2: 'b'}~(4, 5, 6)~['love', 'happiness']^_^
我们已经知道d,t,l会被打包成一个tuple,赋给objects。
应用例子打印数字1到100,3的倍数打印“Fizz”来替换这个数,5的倍数打印“Buzz”,对于既是3的倍数又是5的倍数的数字打印“FizzBuzz”。
for x in range(101):
print("fizz"[x%3*4::]+"buzz"[x%5*4::] or x)
Note:
1. x不是3位数时,fizz切片操作为空串''
2. print()函数中or前面为''时才会输出后面的,否则只输出前面的字符
[FizzBuzz编程练习:60个字符解决FizzBuzz - Jeff Atwood]
print魔法Output caching system
[_] (a single underscore): stores previous output, like Python’sdefault interpreter.
>>> a = 'aaa'
>>> a
'aaa'
>>> print(_)
aaa
在ipython中还另加两个:[__] (two underscores): next previous.[___] (three underscores): next-next previous.
[Output caching system¶]
2.pretty print - pprint()函数
from pprint import pprint # pretty-printer>>> pprint(texts)
Note:
1. pprint 模块( pretty printer )用于打印 Python 数据结构. 当你在命令行下打印特定数据结构时你会发现它很有用(输出格式比较整齐, 便于阅读).e.g.会将输出对象列表中的列表元素自动换行输出。
2. pprint模块不能同时打印两个list( pprint(list1, list2) ),否则会出错。
3. json文档打印
如果你想要漂亮的将文件中的json文档打印出来,你可以用以下这种方式:cat file.json | python -m json.tools
4.格式化输出
打开IDLE输入help(print),我们可以看到如下结果:
- >>> help(print)
- Help on built-in function print in module builtins:
- print(...)
- print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
- Prints the values to a stream, or to sys.stdout by default.
- Optional keyword arguments:
- file: a file-like object (stream); defaults to the current sys.stdout.
- sep: string inserted between values, default a space.
- end: string appended after the last value, default a newline.
- flush: whether to forcibly flush the stream.
1、输出整数。
- >>> print("the length of (%s) is %d" %('Python',len('python')),end="!")
- the length of (Python) is 6!
各个进制数的占位符形式:
%x--- hex 十六进制
%d---dec 十进制
%o---oct 八进制
- >>> number=15
- >>> print("dec-十进制=%d\noct-八进制=%o\nhex-十六进制=%x" % (number,number,number))
- dec-十进制=15
- oct-八进制=17
- hex-十六进制=f
- >>> print ("%.4s " % ("hello world"))
- hell
- >>> print("%5.4s" %("Hello world"))
- Hell
同样对其具体的输出格式也可以写成如下的形式,上面的两个整数可以利用*,来动态代入:
- >>> print("%*.*s" %(5,4,"Hello world"))
- Hell
%A.Bs:A表示输出的总的字符串长度
B表示要输出字符串从开始截取的长度
A<B的时候,输出字符串长度为B(A可以<0 )
A>B的时候前方空格
B>字符串长度时,后面不用空格占位
4、输出浮点数(float)
- >>> print("%10.3f" % 3.141516)
- 3.142
浮点数的输出控制和字符串相似,不过序注意的是.3表示的输出3位小数,最后一面按四舍五入方式进位。
python列表格式化输出
?
[numpy输入输出]
新式类格式化format输出和扩展形式
[python字符串、字符串处理函数及字符串相关操作 ]
[Python字符串格式化输出详细及其扩展形式]
[python中的字符串对齐输出的另一种实现方式]
str()和repr() - 把object转化为str的形式输出
在Python中,字符串(strings)是由内置类str代表的,这是一个类。同时Python还有内置函数str()。
输入输出都是以字符串的形式,print()就是把非str的object转化为其str的形式输出。那么Python怎么把一个object转化为str的形式呢,Python会把这个object传给内置str()函数。
str()回去寻找这个对象的__str__()属性,如果这个对象没有__str__()属性,str()会调用repr()来得到结果。
class wy:
def __str__(self):
return "abc"
w = wy()
print(w)
Note:
1. 如果没有定义__str__(),则会调用repr(wy),会输出:
<__main__.wy2 instance at 0x7f900d0c8050>
2. python2中则是__unicode__()函数
from:http://blog.csdn.net/pipisorry/article/details/39231949
ref:Python输入输出(IO)
Python起步之print & input用法总结
- Python输入输出详解
- python基本语法_输入输出详解
- python输入输出
- Python输入输出
- python输入输出
- Python输入输出
- python输入输出
- Python 输入输出
- Python输入输出
- python 输入输出
- Python输入输出
- python 输入输出
- python输入输出
- Python输入输出
- Python输入输出
- Python输入输出
- python输入输出
- Python输入输出
- log4j日志封装说明—slf4j对于log4j的日志封装-正确获取调用堆栈
- Redis-benchmark使用总结
- ASP中文本文件与数据库文件的数据交换(FSO)
- A group password in Linux
- python 初步学习
- Python输入输出详解
- ASP中文本文件与数据库文件的数据交换(FSO)
- 中文与数字相互转换的函数
- Dialogs(对话框)
- teradata常用语句
- py2exe打包matplotlib和PyQt4
- 活动中心--三分法
- Asp设计常见问题及解答精要
- HTML返回码