I have a multiline string:
>>> import credstash
>>> d = credstash.getSecret('alex_test_key', region='ap-southeast-2')
To see the raw data (first 162 characters):
>>> credstash.getSecret('alex_test_key', region='ap-southeast-2')[0:162]
u'-----BEGIN RSA PRIVATE KEY-----\nMIIEogIBAAKCAQEA6oySC+8/N9VNpk0gJS7Gk8vn9sYN7FhjpAQnoHRqTN/Oaiyx\nxk2AleP2vXpojA/DHldT1JO+o3j56AHD+yfNFFeYvgWKDY35g49HsZZhbyCEAB45\n'
And:
>>> print d[0:162]
-----BEGIN RSA PRIVATE KEY-----
MIIEogIBAAKCAQEA6oySC+8/N9VNpk0gJS7Gk8vn9sYN7FhjpAQnoHRqTN/Oaiyx
xk2AleP2vXpojA/DHldT1JO+o3j56AHD+yfNFFeYvgWKDY35g49HsZZhbyCEAB45
I write to a YAML file:
>>> import yaml
>>> with open('foo.yaml', 'w') as f:
... yaml.dump(d, f, default_flow_style=False, explicit_start=True)
...
Now it looks like this:
$ head -5 foo.yaml
--- !!python/unicode '-----BEGIN RSA PRIVATE KEY-----
MIIEogIBAAKCAQEA6oySC+8/N9VNpk0gJS7Gk8vn9sYN7FhjpAQnoHRqTN/Oaiyx
xk2AleP2vXpojA/DHldT1JO+o3j56AHD+yfNFFeYvgWKDY35g49HsZZhbyCEAB45
i.e. each line has two newlines.
Now if I read it back into a string I see that all is okay in the round-trip:
>>> with open('foo.yaml', 'r') as f:
... d = yaml.load(f)
...
>>> print d[0:162]
-----BEGIN RSA PRIVATE KEY-----
MIIEogIBAAKCAQEA6oySC+8/N9VNpk0gJS7Gk8vn9sYN7FhjpAQnoHRqTN/Oaiyx
xk2AleP2vXpojA/DHldT1JO+o3j56AHD+yfNFFeYvgWKDY35g49HsZZhbyCEAB45
(I don't understand why however.)
My real problem is that if humans read this YAML file they will probably assume, as I did, that my program has broken the formatting of the private key file.
Is there a way to use yaml.dump so as to output something without the additional newline characters?