Python的easyinput模块
Python的easyinput模块提供了一个简单的输入接口,类似于 C++ 中的 cin 流。它支持包括文件在内的多种数据类型,并提供输入到不同数据类型、多行输入等功能。
安装
要安装此模块,请在终端中键入以下命令。
pip install easyinput
函数
- read(*type, amount=1, file, as_list):从输入流中返回标记。
Parameters:
- type : List of data types, for each type corresponding token is returned.
- amount : For repetition of given type for specific number of times.
- as_list : If true, stream of tokens is returned as list, otherwise, generator is returned.
- file : File entity stream.
- read_many(*types, amount=1, file=_StdIn):从输入流中读取标记,除非输入了其他类型的元素。
示例 1:read() 和 read_many() 的工作
Python3
from easyinput import read_many, read
a = read(int)
b = read(str)
print("The elements using read : ")
print(a, b)
print("Integer inputs using read many : ")
for num in read_many(int):
print(num)
# reading the string after integers
print(read())
Python3
from easyinput import read
# input int, str, int chain 3 times
multi_input = read(int, str, amount=2)
# printing type and input
print(type(multi_input))
print(multi_input)
# putting as_list = False
print("Using as_list false : ")
multi_input = read(int, str, amount=2, as_list=False)
print(type(multi_input))
print(multi_input)
Python3
from easyinput import read_many
print("Getting integer inputs from files : ")
with open('gfg_file_input') as inp_file:
for ele in read_many(int, file=inp_file):
print(ele)
Python3
from easyinput import read
class ToLower:
def __init__(self, ele):
self.ele = ele.lower()
def print_ele(self):
print(self.ele)
# Gets object of ToLower class
ele = read(ToLower)
# printing the word
print("Lowercase string : ")
ele.print_ele()
Python3
from easyinput import read_many_lines
print("Reading lines using read many lines : ")
count = 1
for line in read_many_lines():
print(line)
count = count + 1
if count > 5:
break
print("Reading lines using read many lines by skipping empty : ")
count = 1
for sline in read_many_lines(skip_empty=True):
print(sline)
count = count + 1
if count > 5:
break
输出 :
示例 2:使用 amount() 和 as_list()
通常,在 read() 中处理多个参数时, read() 会返回一个列表。如果我们需要使用生成器,而不是列表,则可以将 as_list() 设置为 false。它可以具有的优点是它可以避免列表的迭代来访问和填充不同的数据类型。
蟒蛇3
from easyinput import read
# input int, str, int chain 3 times
multi_input = read(int, str, amount=2)
# printing type and input
print(type(multi_input))
print(multi_input)
# putting as_list = False
print("Using as_list false : ")
multi_input = read(int, str, amount=2, as_list=False)
print(type(multi_input))
print(multi_input)
输出 :
示例 3:使用 read_many() 输入文件
read() 和 read_many() 函数提供了获取文件作为输入流的功能,以从文件中获取数据并在控制台上呈现,或使用file参数到任何文件。
代码 :
蟒蛇3
from easyinput import read_many
print("Getting integer inputs from files : ")
with open('gfg_file_input') as inp_file:
for ele in read_many(int, file=inp_file):
print(ele)
输出 :
示例 4:在 read() 中使用自定义数据类型
除了原始数据类型之外,read() 还可以接受接受字符串作为输入的类实例,这些字符串可用于转换为自定义类型以进行工作。下面的示例获取小写单词。
蟒蛇3
from easyinput import read
class ToLower:
def __init__(self, ele):
self.ele = ele.lower()
def print_ele(self):
print(self.ele)
# Gets object of ToLower class
ele = read(ToLower)
# printing the word
print("Lowercase string : ")
ele.print_ele()
输出 :
使用 read_many_lines()
与 read_many() 类似,不同之处在于它一次读取整行而不是在空格处移动到换行符。读取整行。
句法:
read_many_lines(rstrip=True, skip_empty=False)
Parameters:
rstrip : Skips all the trailing newline characters that are entered. By default value is True.
skip_empty : Defaults to false, when set to True, skips the lines which are just empty characters.
代码 :
蟒蛇3
from easyinput import read_many_lines
print("Reading lines using read many lines : ")
count = 1
for line in read_many_lines():
print(line)
count = count + 1
if count > 5:
break
print("Reading lines using read many lines by skipping empty : ")
count = 1
for sline in read_many_lines(skip_empty=True):
print(sline)
count = count + 1
if count > 5:
break
输出 :