use*_*943 2 python string dictionary
考虑一下这个词:
d = {
   value_1 = 'hello',
   value_2 = False,
   value_3 = 29
}
我想在这样的文件中写这些变量:
value_1 = 'hello'
value_2 = False
value_3 = 29
我试过了:
f.write(
    "\n".join(
        [
            "{key} = {value}".format(**dict(key=k, value=v))
            for k, v in d.items()
        ]
    )
)
但输出是
value_1 = hello  # not a string
value_2 = False
value_3 = 29
使用时应使用repr值的表示.用{!r}在字符串格式化为:
>>> x = 'hello'
>>> print x
hello
>>> print repr(x)
'hello'
>>> print '{!r}'.format(x)
'hello'
演示:
>>> from StringIO import StringIO
>>> c = StringIO()
>>> d = {
...    'value_1' : 'hello',
...    'value_2' : False,
...    'value_3' : 29
... }
>>> for k, v in d.items():
...     c.write("{} = {!r}\n".format(k, v))
...
>>> c.seek(0)     
>>> print c.read()
value_1 = 'hello'
value_3 = 29
value_2 = False