[GRASS-dev] Parsing output of r.category which includes labels

Nikos Alexandris nik at nikosalexandris.net
Mon Aug 20 17:07:41 PDT 2018


* Stefan Blumentrath <Stefan.Blumentrath at nina.no> [2018-08-20 10:48:34 +0000]:

>Hi Nikos,
>
>You could use numpy and genfromtxt() to parse the output string...
>genfromtxt() requires an StringIO object (or file) and StringIO (from io) requires unicode()...
>
>So you could do:
>
>from io import StringIO
>import numpy as np
>output = np.genfromtxt(StringIO(unicode(grass.read_command('r.category', map=base))) , delimiter='\t', dtype=None, names=['cat', 'label'])
>
>That causes however some overhead [1]. So if it makes sense depends on what you want to do with the data in the further processing chain...
>
>Cheers
>Stefan
>
>1: https://docs.scipy.org/doc/numpy/user/basics.io.genfromtxt.html

Thank for the idea Stefan.
This is really a small (in size) task.
I think it's not worth to use NumPy just for a few lines.

Wouldn't the major reason, to use NumPy, be speed in computations?

Nikos
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 228 bytes
Desc: not available
URL: <http://lists.osgeo.org/pipermail/grass-dev/attachments/20180821/26031573/attachment.sig>


More information about the grass-dev mailing list