Suppose I want to read a sequence of inputs, where each input is a tuple is of the form <string> , <integer>, <string>. Additionally, there can be arbitrary amount of whitespace around the commas. An easy way to do this in C/C++ is to use scanf with format string "%s , %d , %s". What is the equivalent function in python?
Suppose we knew that each input is on a separate line, then you could easily parse this in python using split and strip. But the newline requirement complicates things. Furthermore, we could even have weird inputs such as
<s11>, <i1>
, <s12> <s21>,
<i2> , <s22>
Where s11, i1, s12 is the first input and s21, i2, s22 is the second. And scanf would still be able to handle this. How does one do it in python? I also don't want to take the entire input at once and parse it, since I know that there will be other inputs that don't fit this format later on, and I don't want to do the parsing manually.
scanf. The problems you list also appear when (mis)usingscanf. As there is no such function in Python, you indeed have to write one yourself.