I think you can replace the above two lines with:
```
lines.append('\\{0} = {1!r}'.format(keyword.strip(), val))
```
If `val` is a basestring, then it will automatically choose either single or double quotes, depending on whether the string contains a single or double quote character:
```
In [20]: print repr('hi"there')
'hi"there'

In [21]: print repr("hi'there")
"hi'there"
```
If `val` is an int then it's the same.  If `val` is a float then `repr` has the advantage over `str` in that it will print all required digits to fully represent the value.  From Python 2.7 it does this in a nice way, while for Python 2.6 you can sometimes get cases like `1.00000000000015`.  But that's better than the converse where your value is really 1.0000000000015 and with str() you get `1.0`.