sscanf in Python

python parse
python regex
python split
python parse string
python fscanf
python print scanf
python sscanf regex
fgets equivalent in python

I'm looking for an equivalent to sscanf() in Python. I want to parse /proc/net/* files, in C I could do something like this:

int matches = sscanf(
        "%*d: %64[0-9A-Fa-f]:%X %64[0-9A-Fa-f]:%X %*X %*X:%*X %*X:%*X %*X %*d %*d %ld %*512s\n",
        local_addr, &local_port, rem_addr, &rem_port, &inode);

I thought at first to use str.split, however it doesn't split on the given characters, but the sep string as a whole:

>>> lines = open("/proc/net/dev").readlines()
>>> for l in lines[2:]:
>>>     cols = l.split(string.whitespace + ":")
>>>     print len(cols)

Which should be returning 17, as explained above.

Is there a Python equivalent to sscanf (not RE), or a string splitting function in the standard library that splits on any of a range of characters that I'm not aware of?

Python doesn't have an sscanf equivalent built-in, and most of the time it actually makes a whole lot more sense to parse the input by working with the string directly, using regexps, or using a parsing tool.

Probably mostly useful for translating C, people have implemented sscanf, such as in this module:

In this particular case if you just want to split the data based on multiple split characters, re.split is really the right tool.

Is there a scanf() or sscanf() equivalent in python?, There is nothing as such for python. For simple input parsing, the easiest way is usually to split the line into whitespace-delimited words using� There is nothing as such for python. For simple input parsing, the easiest way is usually to split the line into whitespace-delimited words using the split() method of string objects and then convert decimal strings to numeric values using int() or float().

There is also the parse module.

parse() is designed to be the opposite of format() (the newer string formatting function in Python 2.6 and higher).

>>> from parse import parse
>>> parse('{} fish', '1')
>>> parse('{} fish', '1 fish')
<Result ('1',) {}>
>>> parse('{} fish', '2 fish')
<Result ('2',) {}>
>>> parse('{} fish', 'red fish')
<Result ('red',) {}>
>>> parse('{} fish', 'blue fish')
<Result ('blue',) {}>

scanf � PyPI, This python implementation of scanf internally translates the simple scanf format into regular expressions, then returns the parsed values. Usage. scanf.scanf(� How to copy files from directory based on a list in Python? How to convert a python print function on html ; convert string to array ; How to set the value in setLabel function more than once? Creating a program that creates a rectangle with two mouse clicks in Python ; Compare one character from a string with a string.

When I'm in a C mood, I usually use zip and list comprehensions for scanf-like behavior. Like this:

input = '1 3.0 false hello'
(a, b, c, d) = [t(s) for t,s in zip((int,float,strtobool,str),input.split())]
print (a, b, c, d)

Note that for more complex format strings, you do need to use regular expressions:

import re
input = '1:3.0 false,hello'
(a, b, c, d) = [t(s) for t,s in zip((int,float,strtobool,str),'^(\d+):([\d.]+) (\w+),(\w+)$',input).groups())]
print (a, b, c, d)

Note also that you need conversion functions for all types you want to convert. For example, above I used something like:

strtobool = lambda s: {'true': True, 'false': False}[s]

sscanf in Python, Im looking for an equivalent to sscanf in Python I want to parse procnet files in C I could do something like thisint matches sscanf In line 10, sscanf() function is called to read the data from the string pointed to by str. Notice that the string literal "Tom Manager 28" contains three pieces of information name, designation and age separated by space.

You can split on a range of characters using the re module.

>>> import re
>>> r = re.compile('[ \t\n\r:]+')
>>> r.split("abc:def  ghi")
['abc', 'def', 'ghi']

String Parsing (sscanf) — NEURON 7.7 documentation, Syntax: num = h.sscanf("string", "format", args); Description: The Python standard library does not provide a direct equivalent for h.sscanf , but consider using� A = sscanf(str,formatSpec) reads data from str, converts it according to the format specified by formatSpec, and returns the results in an array. str is either a character array or a string scalar. The sscanf function repeatedly applies formatSpec to sequences of characters in str until it either reaches the end of str or fails to match

You can parse with module re using named groups. It won't parse the substrings to their actual datatypes (e.g. int) but it's very convenient when parsing strings.

Given this sample line from /proc/net/tcp:

line="   0: 00000000:0203 00000000:0000 0A 00000000:00000000 00:00000000 00000000     0        0 335 1 c1674320 300 0 0 0"

An example mimicking your sscanf example with the variable could be:

import re
hex_digit_pattern = r"[\dA-Fa-f]"
pat = r"\d+: " + \
      r"(?P<local_addr>HEX+):(?P<local_port>HEX+) " + \
      r"(?P<rem_addr>HEX+):(?P<rem_port>HEX+) " + \
      r"HEX+ HEX+:HEX+ HEX+:HEX+ HEX+ +\d+ +\d+ " + \
pat = pat.replace("HEX", hex_digit_pattern)

values =, line).groupdict()

import pprint; pprint values
# prints:
# {'inode': '335',
#  'local_addr': '00000000',
#  'local_port': '0203',
#  'rem_addr': '00000000',
#  'rem_port': '0000'}

How to simulate scanf() method using Python?, Python does not currently have an equivalent to scanf(). Regular expressions are generally more powerful, though also more verbose, than scanf� According to Python documentation. Python does not currently have an equivalent to scanf(). Regular expressions are generally more powerful, though also more verbose, than scanf()format strings. The table below offers some more-or-less equivalent mappings between scanf() format tokens and regular expressions. scanf() TokenRegular Expression

What is the equivalent of scanf in Python?, There is no direct equivalence, since Python is very different to scanf. scanf ' attempts' to parse the input line and stores the values in the variables, it does no � sscanf in Python. Related. 4960. How do I merge two dictionaries in a single expression in Python (taking union of dictionaries)? 4970. Calling an external command

PHP function.sscanf in Python, The function sscanf() is the input analog of printf(). sscanf() reads from the string str and interprets it according to the specified format , which is described in the�

6.2. re — Regular expression operations — Python 3.3.7 , Simulating scanf()�. Python does not currently have an equivalent to scanf(). Regular expressions are generally more powerful, though also more verbose, than�

  • Is there any reason you are insisting on "not RE"? Regexes are the perfect tool for this job.
  • If you want to program in C, why not program in C? If you want to program in python, use a regular expression. There's even a helpful hint in the documentation for the re module telling you how to convert scanf formats into regular expressions.
  • @Paul, the last bit would have made a great answer.
  • @MattJoiner, I think it would be better to request/disallow features than to request/disallow implementations. "I would like to have format strings that specify the type of the output variable, to have the types converted for me, and to assert specific formatting of the input string" rather than "not regex" explains why you have this preference. After all, if someone used regex to build what you wanted, you'd use it, wouldn't you?
  • @interestinglythere: wat
  • i did say no re, but you justify it nicely
  • here's a py3k version of the linked implementation:
  • I'm very happy for your suggestion, as I was just search for something like this. Weird coincidence.
  • This is the real answer!
  • I really like this approach, especially as my problem was not just a need for scanf, but sscanf.
  • This appeared to be a good solution; sadly bool("false") returns True, because only empty strings evaluate to False. However, all is not lost, you could replace bool with a custom function which behaves the way you'd like.
  • @Aky Nice catch! I fixed my answer.
  • @rookiepig t(s) gets replaced by int(substring1), float(substring2), strtobool(substring3), and str(substring4), in order.
  • it is not a funny to deal with regex on textual float representation