How to sort environment variables in python
Question:
I am printing environment variables, but there are not printed in a sorted way :
for variable in os.environ:
print(" " + variable + "=" + os.environ.get(variable))
I would like to sort os.environ. I tried to put os.environ in a list(), but the sort function is not working.
Any clue?
Thank you
Answers:
for name, value in sorted(os.environ.items()):
print(" " + name + "=" + value)
print 'n'.join(" %-24s= %s" % x for x in sorted(os.environ.items()))
from json import dumps
from os import environ
print(dumps({k: v for k, v in sorted(environ.items())}, indent=2))
I am printing environment variables, but there are not printed in a sorted way :
for variable in os.environ:
print(" " + variable + "=" + os.environ.get(variable))
I would like to sort os.environ. I tried to put os.environ in a list(), but the sort function is not working.
Any clue?
Thank you
for name, value in sorted(os.environ.items()):
print(" " + name + "=" + value)
print 'n'.join(" %-24s= %s" % x for x in sorted(os.environ.items()))
from json import dumps
from os import environ
print(dumps({k: v for k, v in sorted(environ.items())}, indent=2))