sscanf() is from the programming language C and C++. While Python does not offer any exact equivalent method or library for this method, there may be other ways to execute this functionality.
This Python article will make you better understand what
sscanf() has to offer and how we can mimic it in a Python script.
sscanf() Functionality in Python
sscanf() method extracts a string from an already provided string. This is how we declare the method:
int sscanf(const char *str, const char *format, ...)
This method will essentially read formatted input from a string, and unlike
scanf(), the data for
sscanf() is read from a string instead of a console. The data is read from the buffer and into the location addresses provided as arguments in the method declaration.
Every argument provided points towards a variable with the same type as the format string. The method returns the values that were successfully converted and assigned.
There are no direct equivalent libraries or modules for
sscanf() in Python itself. However, there are two ways to mimic the functionality.
Use Regular Expressions From the
A regular expression helps specify a string’s format or describe a string’s format, which can then be used to validate a different string. A regular expression may contain special as well as ordinary characters.
Characters like A, B, b, or 0 are good examples of the simplest of ordinary characters in an expression. The library can also search for specific characters in a string or a list of characters in a particular order.
Look at the example script below that searches for the string
def in a provided string.
import re m = re.search('(?<=abc)def', 'abcdef') m.group(0)
The program returns
def as output.
It is also possible to search for strings within strings separated by special characters. For example, in the script below, we search for the word provided after a hyphen:
m = re.search(r'(?<=-)\w+', 'spam-emails') m.group(0)
The output for this is
emails. The possibilities of parsing with
re are endless!
Another library similar to
regex, which is API-friendly. Regex is backward compatible with
re and comes with additional functionalities.
Following is a conditional pattern test made possible with a
regex module. We will, of course, need to import the library before executing this.
>>> regex.match(r'(?(?=\d)\d+|\w+)', '123abc') <regex.Match object; span=(0, 3), match='123'> >>> regex.match(r'(?(?=\d)\d+|\w+)', 'abc123') <regex.Match object; span=(0, 6), match='abc123'>
Use the Neuron Library
neuron library (not from Python itself) can also be used to import
sscanf() in a Python script. For example, if we were to run the following script,
from neuron import h x = h.ref(0) h.sscanf('0.3', '%f', x) print(x)
The resulting output would be
Here are some more examples we can go through to understand the use of
sscanf() in Python via the
neuron library, with the outputs against each line added as a comment.
from neuron import h as hoc string = hoc.ref('') range_list = [hoc.ref(0) for i in range(50)] hoc.sscanf("This is a test\n", "%s", string) print(string) hoc.sscanf("This is a test\n", "%[^\n]", string) print(string) hoc.sscanf("This is a test\n", "%*s%s", string) print(string) hoc.sscanf("1 2 3 4 5 6 7 8 9 10", "%f%f%f%f%f%f%f%f%f%f%f", range_list, range_list, range_list, range_list, range_list, range_list, range_list, range_list, range_list, range_list, range_list, range_list, range_list) print('Should only have non-zero values for range_list indices 0 - 9') for i in range(13): print('%d %g' % (i, range_list[i]))
As one can observe from both options described above, we can use either library depending on the scenario. Python is an extensive, fast-typed, and beautiful language that offers us a wide range of options to parse strings in a fashion that is typically unique to Python.
regex libraries are examples of the vastness of the language.
We hope you find this article helpful in understanding the basic concept of the
sscanf() function used in Python.