简体   繁体   中英

how do i parse environment variables with pystache?

hi i am trying to run a python shell script that will take all the environment variables and merge them into a template using pystache. now the pystache.render command takes a dictionary argument. unfortunately os.environ does not return a dictionary.

my test case looks like this: TEST=myTest python

import pystache
import os
pystache.render("{{TEST}} HELLO",os.environ)

any assistance would be much appreciated.

Wow. I'm not sure what pystache is doing or why os.environ won't work as is, but you're totally right. This doesn't work:

>>> import pystache
>>> import os
>>> pystache.render("{{PATH}}",os.environ)
u''

But it works fine if you transform os.environ into a dict :

>>> import pystache
>>> import os
>>> pystache.render('{{PATH}}', dict(os.environ))
u'/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM