您如何从Python的stdin中读取信息?
Python supports following ways to read an input from stdin (standard input),
Python支持以下方式从stdin(标准输入)读取输入 ,
1)使用sys.stdin (1) Using sys.stdin)
sys.stdin is a file-like object on which we can call functions read() or readlines(), for reading everything or read everything and split by newline automatically.
sys.stdin是一个类似于文件的对象,我们可以在其上调用函数read()或readlines() ,以读取所有内容或读取所有内容并自动由换行符拆分。
Example:
例:
from sys import stdin
input = stdin.read(1)
user_input = stdin.readline()
amount = int(user_input)
print("input = {}".format(input))
print("user_input = {}".format(user_input))
print("amount = {}".format(amount))
Output
输出量
123
input = 1
user_input = 23
amount = 23
2)使用input() (2) Using input())
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 string (stripping a trailing newline) and returns that.
如果存在提示参数,则将其写入到标准输出中,而无需尾随换行符。 然后,该函数从输入中读取一行,将其转换为字符串(带末尾的换行符)并返回。
Example:
例:
test = input('Input any text here --> ')
print("Input value is: ", test)
Output
输出量
Input any text here --> Hello Readers!
Input value is: Hello Readers!
Recommended posts
推荐的帖子
Read input as an integer in Python
在Python中将输入读取为整数
Read input as a float in Python
在Python中以浮点形式读取输入
Parse a string to float in Python (float() function)
解析要在Python中浮动的字符串(float()函数)
Asking the user for integer input in Python | Limit the user to input only integer value
要求用户在Python中输入整数| 限制用户仅输入整数值
Asking the user for input until a valid response in Python
要求用户输入直到Python中的有效响应
Input a number in hexadecimal format in Python
在Python中以十六进制格式输入数字
Input a number in octal format in Python
在Python中以八进制格式输入数字
Input a number in binary format in Python
在Python中以二进制格式输入数字
How to get the hexadecimal value of a float number in python?
如何在python中获取浮点数的十六进制值?
Convert an integer value to the string using str() function in Python
使用Python中的str()函数将整数值转换为字符串
Convert a float value to the string using str() function in Python
使用Python中的str()函数将浮点值转换为字符串
Input and Output Operations with Examples in Python
使用Python中的示例进行输入和输出操作
Taking multiple inputs from the user using split() method in Python
使用Python中的split()方法从用户获取多个输入
Fast input / output for competitive programming in Python
快速输入/输出,可在Python中进行有竞争力的编程
Precision handling in Python
Python中的精确处理
Python print() function with end parameter
带有结束参数的Python print()函数
翻译自: https://www.includehelp.com/python/how-do-you-read-from-stdin-in-python.aspx
您如何从Python的stdin中读取信息?相关推荐
- python怎么读文件里的指定几行-Python从文件中读取指定的行以及在文件指定位置写入...
Python从文件中读取指定的行 如果想根据给出的行号, 从文本文件中读取一行数据, Python标准库linecache模块非常适合这个任务: 测试文件内容 :This is line 1. Thi ...
- python获取excel某一列-Python从Excel中读取日期一列的方法
如下所示: import xlrd import datetime file=u"伏特加.xls"#注意读中文文件名稍微处理一下 data=xlrd.open_workbook(f ...
- python 文件指定位置写入-Python从文件中读取指定的行以及在文件指定位置写入...
Python从文件中读取指定的行 如果想根据给出的行号, 从文本文件中读取一行数据, Python标准库linecache模块非常适合这个任务: 测试文件内容 :This is line 1. Thi ...
- python怎么读取excel某一列-Python从Excel中读取日期一列的方法
如下所示: import xlrd import datetime file=u"伏特加.xls"#注意读中文文件名稍微处理一下 data=xlrd.open_workbook(f ...
- 【附源码】用Python从文件中读取学生成绩,并计算最高分/最低分/平均分
兄弟们, 今天咱们试试用Python从文件中读取学生成绩, 并计算最高分/最低分/平均分. 涉及知识点 文件读写 基础语法 字符串处理 循环遍历 代码展示 模块 import platform 定义获 ...
- 如何用Python从文件中读取学生成绩,并计算最高分/最低分/平均分(附源码)
兄弟们, 今天咱们试试用Python从文件中读取学生成绩, 并计算最高分/最低分/平均分. 涉及知识点 文件读写 基础语法 字符串处理 循环遍历 代码展示 模块 import platform 定义获 ...
- 用Python从文件中读取学生成绩,并计算最高分/最低分/平均分
兄弟们,今天咱们试试用Python从文件中读取学生成绩,并计算最高分/最低分/平均分. 涉及知识点 文件读写 基础语法 字符串处理 循环遍历 代码展示 模块 import platform 定义获取最 ...
- python 从同花顺获取数据导出,通达信PYTHON读取本地数据,如何使用python在文件中读取数据?...
Q1:如何使用python在文件中读取数据? file = open('a','r').readlines()[1] Q2:python怎么将本地一个文件夹的所有文本读进内存中 列出文件,把各个文件打 ...
- python读取excel一列-Python从Excel中读取日期一列的方法
import xlrd import datetime file=u"伏特加.xls"#注意读中文文件名稍微处理一下 data=xlrd.open_workbook(file) t ...
最新文章
- websocket探究
- Visual Studio 2017 UTF-8 无 BOM 一站式解决办法
- 设置npm的registry
- 2020/5/13号单词
- 【51nod - 1050】循环数组最大子段和(dp)
- grep从文件末尾开始找_新人自学前端到什么程度才能找工作?
- Java i18n - Java中的国际化
- 请教如何维护好iis服务器?
- mysql数据库二级233_2013年计算机等级二级mysql数据库模拟试题及答案
- Boosting Adversarial Attacks with Momentum论文解读
- LayaAir Geolocation 获取地理位置
- 120多套各种类别微信小程序模板源码
- Word中输入10以上带圈数字
- (转)常见数学符号及公式英文发音大全
- 彻底删除双系统的解决办法(UEFI启动)
- 台式计算机机箱型号及价格,顶级组装台式电脑配置清单
- 还原html默认打开方式,怎么还原打开方式,详细教您Win10系统下如何还原程序默认打开方式...
- python 智能识别 商品_python人工智能-图像识别
- No MyBatis mapper was found in '[com.briup.demo]' package. Please check your
- 华为鸿蒙有可能成功吗 (by quqi99)
热门文章
- (八)linux驱动之ioctl的使用
- css不继承父类样式_提升你的Python编程能力,细说类的继承,拿走不谢
- win7安装硬盘后无法启动计算机,硬盘装Win7系统电脑后开机提示DISK BOOT FAILURE怎么办【图文】...
- mysql异常恢复工具_[MySQL异常恢复]mysql ibd文件恢复
- revit找不到附加模块程序集_TensorFlow基础知识——常用模块(一)
- greenplum 存储过程_揭秘!Greenplum并行执行引擎到底是如何工作的?
- Latex 修改公式的的大小
- linux命令之history命令
- 问题 D: 最小生成树II
- 猫哥教你写爬虫 005--数据类型转换-小作业