The sscanf() Functionality in Python

The sscanf() Functionality in Python

The function 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.

the sscanf() Functionality in Python

The 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 re Library

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.

Example Code:

import re
m ='(?<=abc)def', 'abcdef')

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:

Example Code:

m ='(?<=-)\w+', 'spam-emails')

The output for this is emails. The possibilities of parsing with re are endless!

Another library similar to re is 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.

Example Code:

>>> 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

The 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)

The resulting output would be 0.300000011921.

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.

Example Code:

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)
hoc.sscanf("This is a test\n", "%[^\n]", string)
hoc.sscanf("This is a test\n", "%*s%s", string)
hoc.sscanf("1 2 3 4 5 6 7 8 9 10",
           range_list[0], range_list[1], range_list[2], range_list[3], range_list[4], range_list[5], range_list[6],
           range_list[7], range_list[8], range_list[9], range_list[10], range_list[11], range_list[12])
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][0]))

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.

The re and 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.

Author: Abid Ullah
Abid Ullah avatar Abid Ullah avatar

My name is Abid Ullah, and I am a software engineer. I love writing articles on programming, and my favorite topics are Python, PHP, JavaScript, and Linux. I tend to provide solutions to people in programming problems through my articles. I believe that I can bring a lot to you with my skills, experience, and qualification in technical writing.


Related Article - Python Parse

  • Parse HTML Data in Python