2

After parsing a Grass GIS modules inside Python, I am trying to use this dictionary on my next step of raster calculation. But everything from the parsed dictionary is passed as strings.

# original grass command used:
# r.stat= gscript.parse_command('r.info', flags = 'r', map='HC1015.5')

# here is dict output to reuse
r.stat={u'max': u'95.7933959960938', u'min': u'1.41131834257793'}

To be able to use the dictionary, I like to use the dictionary keys directly but their respective values need to be converted into floats.

I have hacked it in this following way:

stat=r.stat.values()
r_max=float(stat[0]); r_min=float(stat[1])

Any Python tips for doing it easily while I am saving the parsed dectionary?

1 Answers1

2

if you want to change dictionary you use Dict Comprehensions

stat={u'max': u'95.7933959960938', u'min': u'1.41131834257793'}

stat = {k: float(v) for k,v in stat.items()}  

print(stat)

Output

{'max': 95.7933959960938, 'min': 1.41131834257793}
Druta Ruslan
  • 7,171
  • 2
  • 28
  • 38