Reputation: 2835
I have a JSON file that looks like this:
{"environment": "production",
"classes":
{"nfs::server": {"exports": ["/srv/share1","/srv/share3"]}}
}
When I run the following code using Python 3.6
fp=open('example.json', 'r')
data=json.load(fp)
print(50*'-')
print(json.dumps(data, indent=4))
print(50*'-')
json.dump(data, sys.stdout, indent=4)
I get the output:
--------------------------------------------------
{
"environment": "production",
"classes": {
"nfs::server": {
"exports": [
"/srv/share1",
"/srv/share3"
]
}
}
}
--------------------------------------------------
{
"environment": "production",
"classes": {
"nfs::server": {
"exports": [
"/srv/share1",
"/srv/share3"
]
}
}
}%
My question is why is the extra %
included in the json.dump
output compared to the json.dumps
string? It is not an artifact of the OS because if I provide a file object instead of sys.stdout
it also gets written to the file.
Upvotes: 5
Views: 21902
Reputation: 2220
The last % is the first character of your console prompt line or a feature of your shell (https://unix.stackexchange.com/questions/167582/why-zsh-ends-a-line-with-a-highlighted-percent-symbol)
Nothing to do with json, nor python.
Because, when print() add a '\n' at the end, the dump to stdout doesn't
Upvotes: 4
Reputation: 9224
The reason is that print(json.dumps(data, indent=4))
prints a newline, and json.dump(data, sys.stdout, indent=4)
does not.
You can try adding a print()
at the end:
print(50*'-')
print(json.dumps(data, indent=4))
print(50*'-')
json.dump(data, sys.stdout, indent=4)
print()
Is the %
symbol part of your shell's prompt?
Upvotes: 3