[GRASS-dev] Access multiple files in a Python script file

Greetings
I’m working on 6.4.0 version and I want to build a Python scripts that acess multiple files. From GRASS7 i.spectral I see that raster variable is a multiple and this is performed.
raster = options[‘raster’]
rastermaps = raster.split(‘,’)

My question is regarding this: how can I access second file for, for instance, do a mapcalc or a for cycle?

Thank you

Luis

Luis Lisboa wrote:

I'm working on 6.4.0 version and I want to build a Python scripts that acess
multiple files. From GRASS7 i.spectral I see that raster variable is a
multiple and this is performed.
raster = options['raster']
rastermaps = raster.split(',')

My question is regarding this: how can I access second file for, for
instance, do a mapcalc or a for cycle?

The .split() method returns a list, so in the above case you can use
e.g.:

  for map in rastermaps:
      ...

--
Glynn Clements <glynn@gclements.plus.com>

Thanks Glynn. Regarding the split function, is is part of GRASS pythin Scripting Library or from normal Python library?
And how can I access an item from the rastermaps list? using rastermaps[0] and rastermaps[1]?

Thank you

On Wed, Feb 17, 2010 at 7:12 PM, Glynn Clements <glynn@gclements.plus.com> wrote:

Luis Lisboa wrote:

I’m working on 6.4.0 version and I want to build a Python scripts that acess
multiple files. From GRASS7 i.spectral I see that raster variable is a
multiple and this is performed.
raster = options[‘raster’]
rastermaps = raster.split(‘,’)

My question is regarding this: how can I access second file for, for
instance, do a mapcalc or a for cycle?

The .split() method returns a list, so in the above case you can use
e.g.:

for map in rastermaps:


Glynn Clements <glynn@gclements.plus.com>

Luis Lisboa wrote:

Thanks Glynn. Regarding the split function, is is part of GRASS pythin
Scripting Library or from normal Python library?

It's part of Python (it's a method of Python's string class).

And how can I access an item from the rastermaps list? using rastermaps[0]
and rastermaps[1]?

That will work; however, if you need to iterate over a list, use:

  for x in list:

or if you need the indices as well as the items:

  for i, x in enumerate(list):

Avoid using C-style iteration, i.e.:

  for i in xrange(len(list)):
      x = list[i]

Operations on Python's sequence types (including strings and lists)
are documented at:

http://docs.python.org/library/stdtypes.html#sequence-types-str-unicode-list-tuple-buffer-xrange

There's a Python tutorial at:

  http://docs.python.org/tutorial/

Also, refer to the existing Python scripts in GRASS 7 for examples:

  http://trac.osgeo.org/grass/browser/grass/trunk/scripts

--
Glynn Clements <glynn@gclements.plus.com>