[GRASS-dev] Parsing output of r.category which includes labels
Stefan.Blumentrath at nina.no
Mon Aug 20 03:48:34 PDT 2018
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 . So if it makes sense depends on what you want to do with the data in the further processing chain...
From: grass-dev <grass-dev-bounces at lists.osgeo.org> On Behalf Of Nikos Alexandris
Sent: søndag 19. august 2018 22:19
To: GRASS-GIS development mailing list <grass-dev at lists.osgeo.org>
Subject: [GRASS-dev] Parsing output of r.category which includes labels
If I am not wrong, all use cases of `read_command()` [0, 1], in (at least) the grass-addons repository, do not consider an output from `r.category` which includes labels.
I work on such a case where category numbers come along with label strings.
To read category numbers, I came up with:
import grass.script as grass
for category in categories:
category = category.split('\t') ```
Is there any other command that will do this better? Would you consider adding one?
More information about the grass-dev