python - storing template replacement values in a separate file -


using string.template want store values substitute template in separate files can loop through.

looping easy part. want run

result = s.safe_substitute(title=titlevar, content=contentvar) 

on template. i’m little stumped in format store these values in text file , how read file python.

what looking call serialization. in case, want serialize dict, such as

values = dict(title='titlevar', content='contentvar') 

there may ways serialize, using xml, pickle, yaml, json formats example. here how json:

import string import json  values = dict(title='titlevar', content='contentvar')  open('/tmp/values', 'w') f:     json.dump(values, f)  open('/tmp/values', 'r') f:     newvals = json.load(f)   s = string.template('''\ $title  $content''') result = s.safe_substitute(newvals) print(result) 

Comments

Popular posts from this blog

curl - PHP fsockopen help required -

HTTP/1.0 407 Proxy Authentication Required PHP -

c# - Resource not found error -