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(
buffer,
"%*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)
1
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: http://hkn.eecs.berkeley.edu/~dyoo/python/scanf/In this particular case if you just want to split the data based on multiple split characters,
re.split
is really the right tool.you can turn the ":" to space, and do the split.eg
no regex needed (for this case)
There is a Python 2 implementation by odiak.
When I'm in a C mood, I usually use zip and list comprehensions for scanf-like behavior. Like this:
Note that for more complex format strings, you do need to use regular expressions:
Note also that you need conversion functions for all types you want to convert. For example, above I used something like:
Update: The Python documentation for its regex module,
re
, includes a section on simulating scanf, which I found more useful than any of the answers above.https://docs.python.org/2/library/re.html#simulating-scanf