首页 > 其他分享 >最全的PyInputPlus模块方法总结

最全的PyInputPlus模块方法总结

时间:2022-11-25 15:03:05浏览次数:29  
标签:pyip 最全 number pyinputplus 模块 Enter PyInputPlus import 输入


最全的PyInputPlus模块方法总结_python

安装第三方模块

最全的PyInputPlus模块方法总结_字符串_02


    在 Windows 和 macOS 中,pip 会随着 Python 自动安装。可以通过命令行窗口输入 pip 检查是否已经安装。但在 Linux 中,必须由你单独安装。在 Ubuntu Linux 或 Debian Linux 上使用 sudo apt-get install python3-pip 安装,在 Fedora Linux 上使用 sudo yum install python3-pip 安装。

    使用 pip install --user MODULE 安装相应的第三方模块。输入验证需要安装的模块为 PyInputPlus ,所以我们先使用 pip install --user pyinputplus 命令安装。


最全的PyInputPlus模块方法总结_python

PyInputPlus 模块中的方法

最全的PyInputPlus模块方法总结_字符串_02






inputStr()





此方法类似于 Python 内置的 input() 函数,但具有一般的 PyInputPlus 功能,例如有效输入时间、有效输入次数等。

>>> import pyinputplus as pyip
>>> result = pyip.inputStr('Enter name> ')
Enter name> Caizi
>>> result
'Caizi'






inputCustom()





此方法类似于 Python 内置的 input() 函数,但具有一般的 PyInputPlus 功能,例如有效输入时间、有效输入次数等。可以传入一个用于验证输入的函数。如果引发异常,验证将失败,并向用户显示异常消息。

>>> import pyinputplus as pyip
>>> def raiseIfUppercase(text):
if text.isupper():
raise Exception('Input cannot be uppercase.')




>>> x = pyip.inputCustom(raiseIfUppercase)
CAIZI
Input cannot be uppercase.
Caizi
>>> x
'Caizi'






inputNum()





提示用户输入整数或浮点数。方法返回 int 或 float 值(取决于用户是否输入了小数点)。其他公共参数说明:

· min 如果不为 None ,则表示接受的最小数值,包括最小参数。

· max 如果不为 None ,则表示接受的最大数值,包括最大参数。

· greaterThan 如果不为 None ,则为接受的最小数值,不包括 greaterThan 参数。

· lessThan 如果不为 None ,则为接受的最大数值,不包括 lessThan 参数。


>>> import pyinputplus as pyip
>>> x = pyip.inputNum('Enter a number> ')
Enter a number> 2.3
>>> x
2.3
>>> x = pyip.inputNum('Enter a number> ')
Enter a number> 2
>>> x
2


>>> import pyinputplus as pyip
>>> x = pyip.inputNum('Enter a number(min=10)> ', min=10)
Enter a number(min=10)> 9
Number must be at minimum 10.
Enter a number(min=10)> 10
>>> x
10


>>> import pyinputplus as pyip
>>> x = pyip.inputNum('Enter a number(max=10)> ', max=10)
Enter a number(max=10)> 11
Number must be at maximum 10.
Enter a number(max=10)> 10
>>> x
10


>>> import pyinputplus as pyip
>>> x = pyip.inputNum('Enter a number greater than 5> ', greaterThan=5)
Enter a number greater than 5> 5
Number must be greater than 5.
Enter a number greater than 5> 6
>>> x
6


>>> import pyinputplus as pyip
>>> x = pyip.inputNum('Enter a number less than 5> ', lessThan=5)
Enter a number less than 5> 5
Number must be less than 5.
Enter a number less than 5> 4
>>> x
4






inputInt()





提示用户输入整数值,以整数形式返回。其他公共参数说明:

· limit:一个整数值,设置在函数退出前最多能有几次有效输入。

· timeout:一个整数值,设置用户只能在几秒内提供有效输入。

· default:设置默认返回值,而不是引发异常。


>>> import pyinputplus as pyip
>>> x = pyip.inputInt('Enter a integer number> ')
Enter a integer number> 2.3
'2.3' is not an integer.
Enter a integer number> 2.0
>>> x
2
>>> x = pyip.inputInt('Enter a integer number> ', limit=2, default='N/A')
Enter a integer number> 2.3
'2.3' is not an integer.
Enter a integer number> 2.1
'2.1' is not an integer.
>>> x
'N/A'
>>> x = pyip.inputInt('Enter a integer number> ', timeout=5, default='N/A')
Enter a integer number> 4 # 输入数字后等待超过5秒再回车
>>> x
'N/A'






inputFloat()





提示用户输入浮点值,以浮点形式返回。

>>> import pyinputplus as pyip
>>> x = pyip.inputFloat('Enter a float number> ')
Enter a float number> 3
>>> x
3.0






inputChoice()





提示用户输入提供的选项之一,以字符串形式返回选定的选项。

>>> import pyinputplus as pyip
>>> x = pyip.inputChoice(['apple', 'pear'])
Please select one of: apple, pear
banana
'banana' is not a valid choice.
Please select one of: apple, pear
pear
>>> x
'pear'






inputMenu()





提示用户输入提供的选项之一,还显示带有项目符号、编号或字母选项的小菜单,以字符串形式返回选定的选项。


未指定任何参数,输入不区分大小写。

>>> import pyinputplus as pyip
>>> x = pyip.inputMenu(['apple', 'pear'])
Please select one of the following:
* apple
* pear
apple
>>> x
'apple'
>>> x = pyip.inputMenu(['apple', 'pear'])
Please select one of the following:
* apple
* pear
PEAR
>>> x
'pear'


指定了数字标号,可以直接输入数字进行选择。

>>> import pyinputplus as pyip
>>> x = pyip.inputMenu(['apple', 'pear'], numbered=True)
Please select one of the following:
1. apple
2. pear
2
>>> x
'pear'


指定了字母标号,可以直接输入字母进行选择。

>>> import pyinputplus as pyip
>>> x = pyip.inputMenu(['apple', 'pear'], lettered=True)
Please select one of the following:
A. apple
B. pear
B
>>> x
'pear'


指定了区分大小写,输入必须与选项大小写一致。

>>> import pyinputplus as pyip
>>> x = pyip.inputMenu(['apple', 'pear'], caseSensitive=True)
Please select one of the following:
* apple
* pear
PEAR
'PEAR' is not a valid choice.
Please select one of the following:
* apple
* pear
pear
>>> x
'pear'






inputDate()





提示用户在“格式”列表中输入指定格式的日期,返回 datetime.date 对象。

>>> import pyinputplus as pyip
>>> x = pyip.inputDate()
2021/08/03
>>> x
datetime.date(2021, 8, 3)
>>> x = pyip.inputDate()
08/03/2021
>>> x
datetime.date(2021, 8, 3)
>>> x = pyip.inputDate(formats=['%b %Y'])
Aug 2021
>>> x
datetime.date(2021, 8, 1)






inputDatetime()





提示用户在“格式”列表中输入指定格式的日期时间,返回 datetime.datetime 对象。

>>> import pyinputplus as pyip
>>> x = pyip.inputDatetime()
2021/08/03 20:00:01
>>> x
datetime.datetime(2021, 8, 3, 20, 0, 1)






inputTime()





提示用户在“格式”列表中输入指定格式的时间,返回 datetime.time 对象。

>>> import pyinputplus as pyip
>>> x = pyip.inputTime()
20:00:01
>>> x
datetime.time(20, 0, 1)






inputUSState()





提示用户输入美国州名称或缩写,我们基本用不上。返回状态缩写(除非 returnStateName 参数为 True ,在这种情况下,将返回完整名称)。

>>> import pyinputplus as pyip
>>> x = pyip.inputUSState()
ca
>>> x
'CA'
>>> x = pyip.inputUSState(returnStateName=True)
ca
>>> x
'California'






inputMonth()





提示用户输入月份名称,返回所选月份名称的字符串。

>>> import pyinputplus as pyip
>>> x = pyip.inputMonth()
8
>>> x
'August'
>>> x = pyip.inputMonth()
august
>>> x
'August'






inputDayOfWeek()





提示用户一周中的某一天,返回星期的名称。

>>> import pyinputplus as pyip
>>> x = pyip.inputDayOfWeek()
tue
>>> x
'Tuesday'






inputDayOfMonth()





提示用户输入从1到28、30或31的数字月份(或闰年为29),取决于给定的月份和年份。以整数形式返回输入的日期。其他参数说明:

· year:给定的年份,它决定了月份中天数的范围。

· month:给定的月份,它决定了可以选择的天数范围。

>>> import pyinputplus as pyip
>>> x = pyip.inputDayOfMonth(2021, 8)
3
>>> x
3
>>> x = pyip.inputDayOfMonth(2000, 2)
29
>>> x
29
>>> x = pyip.inputDayOfMonth(2001, 2)
29
'29' is not a day in the month of February 2001
2
>>> x
2






inputIP()





提示用户输入 IPv4 或 IPv6 地址,以字符串形式返回输入的 IP 地址。

>>> import pyinputplus as pyip
>>> x = pyip.inputIP()
192.168.0.1
>>> x
'192.168.0.1'






inputRegex()





提示用户输入与提供的正则表达式字符串(或正则表达式对象)和标志匹配的字符串,返回输入的字符串。

>>> import pyinputplus as pyip
>>> x = pyip.inputRegex('\d\d')
2
'2' does not match the specified pattern.
23
>>> x
'23'






inputRegexStr()





提示用户输入正则表达式字符串(只支持 Python 风格正则表达式,不支持 Perl 或 JavaScript 风格),返回输入的正则表达式字符串。

>>> import pyinputplus as pyip
>>> x = pyip.inputRegexStr()
\d\d
>>> x
re.compile('\\d\\d')






inputURL()





提示用户输入 URL ,以字符串形式返回 URL 。

>>> import pyinputplus as pyip
>>> x = pyip.inputURL()
Caizi
'Caizi' is not a valid URL.
https://www.baidu.com
>>> x
'https://www.baidu.com'






inputYesNo()





提示用户输入 yes / no,用户还可以输入 y / n 。根据用户的选择返回自定义的“yesVal”或“noVal”参数(默认为“yes”和“no”)。

>>> import pyinputplus as pyip
>>> x = pyip.inputYesNo()
yes
>>> x
'yes'
>>> x = pyip.inputYesNo()
n
>>> x
'no'
>>> x = pyip.inputYesNo(yesVal='yeah', noVal='no')
y
>>> x
'yeah'






inputBool()





提示用户输入 True / False ,用户还可以在任何情况下输入 t / f ,返回一个布尔值。

>>> import pyinputplus as pyip
>>> x = pyip.inputBool()
false
>>> x
False
>>> x = pyip.inputBool()
t
>>> x
True






inputZip()





提示用户输入3到5位数的美国邮政编码,这个基本也用不上。以字符串形式返回 zipcode 。

>>> import pyinputplus as pyip
>>> x = pyip.inputZip()
123
>>> x
'123'






inputFilename()





提示用户输入文件名,文件名不能包含:

\\  /   :  *    ?  "   <   >  |    或者以空格结束


请注意,这将验证文件名,而不是文件路径。/ 和 \\ 字符对于文件名无效,以字符串形式返回文件名。

>>> import pyinputplus as pyip
>>> x = pyip.inputFilename()
C:\Users\1.txt
'C:\\Users\\1.txt' is not a valid filename.
1.txt
>>> x
'1.txt'






inputFilepath()





提示用户输入文件路径,如果 mustExist 为 True ,则此文件路径必须存在于本地文件系统上,以字符串形式返回文件路径。

>>> import pyinputplus as pyip
>>> x = pyip.inputFilepath()
C:\Users
>>> x
'C:\\Users'






inputEmail()





提示用户输入电子邮件地址,以字符串形式返回电子邮件地址。

>>> import pyinputplus as pyip
>>> x = pyip.inputEmail()
Caizi
'Caizi' is not a valid email address.
[email protected]
>>> x
'[email protected]'






inputPassword()





提示用户输入密码。将显示掩码字符而不是实际的字符。

· 如果 correctPassword 为 None ,则任何输入由 inputPassword() 接受并返回。

· strip 的默认值为 '' ,因此不会出现空白。

· 默认情况下, limit 设置为 1 ,因此错误的密码尝试会导致引发 RetryLimitException 。

· 如果将 limit 设置为 None ,则会再次要求用户输入正确的密码。每当用户输入错误的密码时,都会显示 ErrorPasswordMsg 字符串。

· 如果 correctPassword 设置为 None ,则接受所有输入。

· mask 是用于显示的保护密码字符,而不是实际的字符。它可以设置为 None (不隐藏字符)、空白字符串(用户键入时不显示任何内容)或单个字符串(显示此字符而不是按键)。不能将其设置为多字符的字符串。


# 测试时请切回命令行界面运行!
>>> import pyinputplus as pyip
>>> x = pyip.inputPassword()
******
>>> x
'123456'






标签:pyip,最全,number,pyinputplus,模块,Enter,PyInputPlus,import,输入
From: https://blog.51cto.com/u_15891283/5886737

相关文章

  • 最全的Python海龟绘图总结
    在Python中使用海龟绘图,需要导入相应的模块,那么什么是模块呢?逻辑上来说模块就是一组功能的组合;实质上一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上......
  • Python入门(4)——最全的字符串操作
    修改字符串大小写title():把字符串中每个单词首字母改为大写。upper():把字符串中所有字符改为大写。lower():把字符串中所有字符改为小写。>>>'theintroductiontopython'.......
  • WeNet和ESPnet中下采样模块(Conv2dSubsampling)
    关于WeNet和ESPnet两个工具下采样模块都是相同的操作,首先将输入序列扩充一个维度(因为要使用二维卷积),然后通过两个二维卷积,其中第一个卷积的输入通道为“1”,输出通道为odi......
  • 06.使用包、单元包以及模块
    包(package):一个用于构建、测试并分享单元包的Cargo功能;单元包(crate):一个用于生成库或可执行文件的树形模块结构;模块(module)及use关键字:它们被用于控制文件结构、作用域及路......
  • Oracle中字符串截取最全方法总结
    substr函数:截取字符串语法:SUBSTR(string,start, [length])string:表示源字符串,即要截取的字符串。start:开始位置,从1开始查找。如果start是负数,则从string字符串末......
  • 时间序列 工具库学习(18)adtk模块-异常类型
    1.异常类型异常是一个广义的概念,它可以指代时间序列中许多不同类型的事件。根据具体情况,价值飙升、波动性转变、违反季节性模式等都可能是异常的或正常的。ADTK提供了一组......
  • 最全iOS上架详细版本
    众所周知,在开发苹果应用时需要使用签名(证书)才能进行打包安装苹果IPA,作为刚接触ios开发的同学,只是学习iosapp开发内测,并没有上架appstore需求,对于苹果开发者账号认证需要支......
  • centos安装php模块之后还是提示not found解决方法
    我的centos服务器是7.3版本,我装了php5.6版本,然后再装memcached等扩展的时候,一直用不了,方法没找到。在网上找了很多资料,都说重启nginx服务器,然而行不通。。。。正确做法应该......
  • 动环监控模块--关于数壳
    动环监控模块的一些功能特性:通过温湿度传感器,监控机房内各个位置的温湿度情况,实时显示机房内温湿度,保持良好的运行环境,能使服务器发挥更好的性能,可以根据温湿度的变化,了......
  • Node.js使用path模块处理文件路径
    首先需要引入path模块varpath=require('path');1.文件路径处理:path.normalize(p)。path模块中的normalize()方法用来规范化路径字符串。可用于处理路径中的”//”、”........