I just started using Python's ConfigFile.SafeConfigParser class to parse a config file that contains variable definitions and references. It works nicely, but I can't find an explanation for the weird syntax for a variable reference:
BASEPATH = C:\Users\me\x
SOME_FILE_PATH = %(BASEPATH)s\a
# Yields C:\Users\me\x\a
What's the 's' for? Are there other characters that make the variable behave differently?
The syntax comes from Python's string formatting syntax, which is based on C printf syntax.
In short, %s
means "substitute with a s tring", %(abc)s
means "substitute with a string named abc
":
For example:
>>> print "Hello %s!" % 'world'
Hello world!
>>> print "Hello %(name)s!" % dict(name='world')
Hello world!
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.