首页 > 编程语言 >Python中标准输入(stdin)、标准输出(stdout)、标准错误(stdout)的用法

Python中标准输入(stdin)、标准输出(stdout)、标准错误(stdout)的用法

时间:2023-07-06 20:36:15浏览次数:36  
标签:stdout Python 3.14 sys 标准 123 print PI

1.标准输入

input()、raw_input()

Python 3.x 中 input() 函数可以实现提示输入,python 2.x 中要使用 raw_input(),例如:

foo = input("Enter: ")  # python 2.x 要用 raw_input()
print("You input: [%s]" % (foo))

# 测试执行
Enter: abc de
You input: [abc de]     # 读取一行(不含换行符)

sys.stdin

使用 sys.stdin 可以获取标准输入的文件句柄对象,例如:

import sys

print("Enter a line: ")
line = sys.stdin.readline()     # 读取一行(包括换行符)
print("Line: [%s]\n%s" % (line, "-"*20))

print("Enter a character: ")
char = sys.stdin.read(1)        # 读取一个字节
print("Char: [%s]\n%s" % (char, "-"*20))

print("Enter a multi-lines: ")
lines = sys.stdin.read()        # 读取到文件尾
print("Lines: [%s]" % (lines))


# 测试执行
Enter a line:
This is a single line   <======== 输入了一行,然后回车
Line: [This is a single line
]                       <======== 输出有换行符
--------------------
Enter a character:
abc                     <======== 输入了三个字节
Char: [a]               <======== 只读取了一个字节
--------------------
Enter a multi-lines:
first line
second line
last line               <======== 输入三行(换行)后,windows 下按 Ctrl+Z,linux 下按 Ctrl + D 结束输入
Lines: [bc              <======== 上一次未读完的三个字节(两个字符 + 一个换行符)
first line
second line
last line
]                       <======== 最后一行也有换行符

2.标准输出

print

print 可以自动换行,例如:

print("%s is %0.2f, %d is a integer" % ("PI", 3.14, 123))   # 格式同 C 语言中的 printf()
print("{0} is {1}, {2} is a integer".format("PI", 3.14, 123))
print("{foo} is {bar}, {qux} is a integer".format(foo="PI", bar=3.14, qux=123))
#Python小白学习交流群:711312441
# 测试执行
PI is 3.14, 123 is a integer
PI is 3.14, 123 is a integer
PI is 3.14, 123 is a integer

sys.stdout

使用 sys.stdout 可以获取标准输出的文件句柄对象,例如:

import sys
sys.stdout.write("%s is %0.2f, %d is a integer\n" % ("PI", 3.14, 123))   # 格式同 C 语言中的 printf()
sys.stdout.write("{0} is {1}, {2} is a integer\n".format("PI", 3.14, 123))
sys.stdout.write("{foo} is {bar}, {qux} is a integer\n".format(foo="PI", bar=3.14, qux=123))

执行结果与 print 的示例一样。(注:write()不会自动换行,这里加了换行符)

3.标准错误

sys.stdout

使用 sys.stderr 可以获取标准错误的文件句柄对象,示例略(将 sys.stdout 中示例中的 stdout 替换为 stderr 即可)。

标签:stdout,Python,3.14,sys,标准,123,print,PI
From: https://www.cnblogs.com/xxpythonxx/p/17533266.html

相关文章

  • Python中os.system()、subprocess.run()、call()、check_output()的用法
    1.os.system()os.system()是对C语言中system()系统函数的封装,允许执行一条命令,并返回退出码(exitcode),命令输出的内容会直接打印到屏幕上,无法直接获取。示例:#test.pyimportosos.system("ls-l|greptest")#允许管道符#测试执行$ll<=======......
  • Python中startswith()和endswith()方法
    startswith()方法startswith()方法用于检索字符串是否以指定字符串开头,如果是返回True;反之返回False。endswith()方法endswith()方法用于检索字符串是否以指定字符串结尾,如果是则返回True;反之则返回Falses='helloword'print("s.startswith('wor'):",s.startswith('wor......
  • 【Python】多维列表变为一维列表的方法--numpy
    转载:(18条消息)【Python】多维列表变为一维列表的方法_四维列表变一维_Vincent__Lai的博客-CSDN博客题目给定一个多维列表,怎么让其变为一维?例如,输入:[[1,4],[2],[3,5,6]],输出:[1,4,2,3,5,6]常规一行做法a=[[1,4],[2],[3,5,6]]a=[jforiinaforjini......
  • 将PYTHON包环境从一个电脑拷贝到另外一个电脑
    将PYTHON包环境从一个电脑拷贝到另外一个电脑1、在当前电脑复制D:\ProgramFiles\Python\Python311\Lib中的所有文件生成myrequirement.txt文件pipfreeze>myrequirement.txtmyrequirement.txt文件如下:colorama==0.4.6constantly==15.1.0cpca==0.5.5cryptography=......
  • python pydoc模块生成html网页版内容
    pydoc是一个能生成网页版的模块,内置模块命令:python-mpydoc-p1234-m加载模块-p网页访问端口命令行:b打开浏览器q退出效果:Windows环境下:python-mpydoc-watexit//在当前目录创建atexit.htmlpython-mpydoc-p5000//启动一个Web服务器监听h......
  • CPython, Pypy, MicroPython...还在傻傻分不清楚?
    哈喽大家好,我是咸鱼当我们说Python时,通常指的是官方实现的CPython但还有很多比如Pypy、Jython、MicroPython、Brython、RustPython等“python”许多小伙伴看到这些带“python”的概念可能一头雾水,心想这跟我平时接触到的python有什么区别吗?这些到底是什么那么今天这......
  • Python如何实现docstring
    docPython语言从排版上看强制要求了一些书写规范,算是强制赋予了每个程序员一个"代码洁癖"。作为规范的一部分,可以在在类函数的开始增加注释,并且语言本身为这种注释做了"背书":可以通过help展示这个帮助文档的内容。这个本来是Python一个很细小的功能,也是一个很有意思的语法糖(因......
  • Python3读写TOML文件
    TOML(Tom'sObvious,MinimalLanguage)是一种易于阅读和编写的配置文件格式。它的设计目标是提供一种简单而灵活的方式来表示配置数据,以便于人类阅读和编辑。基础示例#config.toml[server]host="localhost"port=8080[database]name="mydb"user="myuser"passwor......
  • Python常用命令总结
    1.print()默认是print(end='\n')  如果不想换行可以print(end='')2.使print内容变成一行print(end='\t')3.不设置指定位置,按默认顺序"{}{}".format("hello","world")  eg打印99乘法表print({}*{}={}\t'.format(i,j,i*j),end......
  • python 并发编程之线程
    一、队列的使用1、在python中,内置的有一个类,Queue就是队列2、队列的使用frommultiprocessingimportQueueif__name__=='__main__':q=Queue(3)#队列的大小默认很大#1.如何入队、"""obj,block=True,timeout=None"""q.put('hellow......