parsing - sscanf in Python -
i'm looking equivalent sscanf() in python. want parse /proc/net/* files, in c 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 @ first use str.split, doesn't split on given characters, sep string whole:
>>> lines = open("/proc/net/dev").readlines() >>> l in lines[2:]: >>> cols = l.split(string.whitespace + ":") >>> print len(cols) 1 which should returning 17, explained above.
is there python equivalent sscanf (not re), or string splitting function in standard library splits on of range of characters i'm not aware of?
python doesn't have sscanf equivalent built-in, , of time makes whole lot more sense parse input working string directly, using regexps, or using parsing tool.
probably useful translating c, people have implemented sscanf, such in module: http://hkn.eecs.berkeley.edu/~dyoo/python/scanf/
in particular case if want split data based on multiple split characters, re.split right tool.
Comments
Post a Comment