sys.stdin.read和raw_input函数
sys.stdin.read函数
例子:
import sysread=sys.stdin.read()
for i in range(len(read)):print i,read[i],-1
运行,当执行到read=sys.stdin.read()
会阻塞,等待我们输入
我们输入:
h e
当输入,ctrl+D结束输入,这个时候ctrl+D是在换行后输入的,运行结果为:
我们只输入了三个字符,却输出来四个字符,重点看3后面,发现是换行,这是因为sys.stdin.read读取完数据后,会在数据后面自动加上’\n’,所以会输出四个字符
当我们ctrl+D直接在e后面输入,不换行输入了,运行结果为
我们会发现什么都没输出
当我们输入:
h e
12
ctrl+D换行之后输入
把12也读入进来了,所以sys.stdin.read是读取多行,当在2后面输入ctrl+D时,输出结果为:
所以ctrl+D哪行,sys.stdin.read不读取。
到此我们可以总结
- 会在输入内容的后面自动加上’\n’
- 读取多行
- ctrl+D结束读取
raw_input函数
程序:
# -*- coding:utf-8 -*-read=raw_input("请输入内容:")
for i in range(len(read)):print i,read[i],-1
raw_input将所有输入作为字符串看待,返回字符串类型。
可以看到会先将括号里的内容先输出,然后获取我们输入内容,当我们按下enter键,输出我们输入的内容,与sys.stdin.read相比没有加上’\n’
sys.stdin.read和raw_input函数相关推荐
- Python的sys.stdout、sys.stdin重定向
Python的sys.stdout.sys.stdin重定向 转自:http://www.cnblogs.com/turtle-fly/p/3280519.html 本文环境:Python 2.7 使 ...
- 18 常用模块 random shutil shevle logging sys.stdin/out/err
random:随机数 (0, 1) 小数:random.random() ***[1, 10] 整数:random.randint(1, 10) *****[1, 10) 整数:random.rand ...
- python中sys.stdout、sys.stdin
如果需要更好的控制输出,而print不能满足需求,sys.stdout,sys.stdin,sys.stderr就是你需要的. 1. sys.stdout与print: 在python中调用print ...
- python3 sys.stdin.readline input 区别
sys.stdin.readline( )会将标准输入全部获取,包括末尾的'\n', input()会把'\n'忽略 import sys a=sys.stdin.readline() b=input ...
- python 中的 sys.stdin ,sys.stdout 和sys.stderr
文档解释: File objects used by the interpreter for standard input, output and errors: stdin is used for ...
- 【python ACM 输入输出的处理:sys.stdin.readline().strip().split())】
[python & ACM 输入输出的处理:sys.stdin.readline().strip().split())] 一.输入处理 按行读取数据 readline() 会返回一个字符串: ...
- Python raw_input 函数 - Python零基础入门教程
目录 一.前言 二.Python input 和 raw_input 区别 1.当输入为纯数字时 2.当输入字符串为表达式 三.Python input 函数实现原理 四.猜你喜欢 基础 Python ...
- pythonstdin_python 笔试输入:sys.stdin.readline和input
①:输入一行数据并输 出两种方法 # 输入一行数据并输出 import sys # 方法一: str1 = input() print('input 输入:',str1,'len=',len(str1 ...
- python input sys.stdin
import sys a=sys.stdin.readline() # 包含了换行符,一般都要使用strip b=input() # 不包含换行符`\n` print(len(a),len(b)) # ...
最新文章
- R语言:生成正态分布数据生成--rnorm,dnorm,pnorm,qnorm
- 对于数据库连接池的一些思考和MyBatis的集成与使用
- python安装教程mac-Mac 安装python 3.*新版本的详细步骤
- 极速生成缩略图,Serverless 支撑赛事转播锁定冬奥亮点
- python 列表,元祖,字典的区别
- 白洁血战Node.js并发编程 01 状态机
- spring整合activeMQ遇到异常:Error creating bean with name 'connectionFactory'
- 模型压缩案例-SSDYou only look once
- Maven中几张重要的总结图
- Mybatis逆向工程(生成实体类)开发指南
- 计算机图形学入门(十五)-光线追踪(辐射度量学)
- android 怎样扩大内存卡,SD卡变RAM 增加虚拟内存方法
- springboot-3-整合ehcache缓存
- 国际贸易通用邮箱,实时邮件配送
- word取消英文首字母自动大写问题
- html圆角半径,CSS3 / 绘制四个角不同半径的圆角边框 - 汇智网
- directives 引用this
- 算法---DFS和BFS
- 云服务器一般用什么系统,云服务器用什么系统好
- 初学VUE 走马灯效果