Package: pyscanf
pyscanfA Python package to read formatted input from the standard input in C style.Jump toinstallationorusage.IntroductionStraight up with an example. When you need to read any kind of input in Python, like the one below, how uncomfortable do you think it is?# Input:# 5 4# 1001 12 girl 2.6# 1002 23 boy 2.2# 1003 23 girl 2.2# 1004 45 boy 10.3# 1005 3 girl 12.0n,m=map(int,input().split())matrix=[]foriinrange(n):m=input().split()matrix.append([int(m[0]),int(m[1]),m[2],float(m[3])])foriinrange(n):print(matrix[i])# Output:# [1001, 12, 'girl', 2.6]# [1002, 23, 'boy', 2.2]# [1003, 23, 'girl', 2.2]# [1004, 45, 'boy', 10.3]# [1005, 3, 'girl', 12.0]I do not mean that usingmapandsplit, and parsing is wrong or bad in any sense. The only thing I want to show you is that you can write this instead:n,m=scanf("%d%d")matrix=[]foriinrange(n):matrix.append(scanf("%d%d%s%f"))Still not impressed? Here is what you also get:n=scanf("%d")m=scanf("%d")matrix=[]foriinrange(n):matrix.append(scanf("%d%d%s%f"))And both of these examples work for the initial input entered from keyboard while executing the code:5 4
1001 12 girl 2.6
1002 23 boy 2.2
1003 23 girl 2.2
1004 45 boy 10.3
1005 3 girl 12.0InstallationTo install and use the latestpyscanfin your python project, run:$ pip install pyscanfUsageImportpyscanfpyscanfhas no dependencies and is basically just a single function. A simple import will get you running:frompyscanfimportscanfBasic usageThescanf()function is documented to give you usage instructions inside your editor. So, just callscanf()and provide it amatch(string) query, which is essentially a string with format specifiers to parse the input:n=scanf("%d")Format specifiersInput is read character by character untilmatchquery is exhausted. Supported format specifiers are (note that some are different from standard Python format specifiers):%d(int),%r(bool),%f(float),%x(complex number).%s(string/char),RaisesValueErrorwhen format specifier and read value did not match.
RaisesTypeErrorwhen match query did not have any valid format specifiers.The number of characters read by each format specifier is limited bylimit. Default is 256.Read character by characterWhat makespyscanfa successor to the regular way of reading input in Python is its way to read character by character from stdin. Given an input:32 11.50 fooYou can read it by either:a,b,s=scanf("%d%f%s")Or in separate lines even though the input was typed in a single file. This is the core similarity with C'sscanffunction:a=scanf("%d")b=scanf("%f")s=scanf("%s")Overall,pyscanfcan make reading input in Python much more intuitive and straightforward. And requires less overhead with ensuring correct variable types.# Input:# 5 4# 1001 12 girl 2.6# 1002 23 boy 2.2# 1003 23 girl 2.2# 1004 45 boy 10.3# 1005 3 girl 12.0n,m=scanf("%d%d")matrix=[]foriinrange(n):matrix.append(scanf("%d%d%s%f"))forxinmatrix:assert(type(x[0])isintandtype(x[1])isintandtype(x[2])isstrandtype(x[3])isfloat)print(x)# Output:# [1001, 12, 'girl', 2.6]# [1002, 23, 'boy', 2.2]# [1003, 23, 'girl', 2.2]# [1004, 45, 'boy', 10.3]# [1005, 3, 'girl', 12.0]Future updatesSo far,pyscanfmeats its initial creation intentions. But this does not mean that improvements are not welcome. Here, we appreciate the opposite. Below is the list of possible additions topyscanf, but you can always open a pull request with your own suggestions as long as they meetpyscanf's guidelines (see below):Parse a given string or a file.Use regex to parse input. Similar to whatparsecan do.Your feature here.Contributing guidelinespyscanfis a small Python package. Its main purpose is to serve a comfortable and easy way to read input in Python. Its main goal is to not necessarily be as feature-rich as possible, but to be ultra compact and applicable for reading input.