Issue
I am getting a '%' character when I cat the file. When I open this using sublime text or vim, this '%' character does not show up. I am using the following code to generate my json.txt file:
import json
filename = "json.txt"
with open(filename, "r+") as f:
x = [1, 'simple', 'list']
json.dumps(x)
json.dump(x, f)
Solution
If you run cat
and see %
, as DroidX86 has said, it means that there is no a new line at the end of the file, you can avoid this problem using the following code:
import json
filename = "json.txt"
with open(filename, "r+") as f:
x = [1, 'simple', 'list']
print(json.dumps(x, indent=4),file=f)
Now, if you run cat
, you no longer will see the %
since the function print
adds a new line for you.
Answered By - lmiguelvargasf Answer Checked By - Candace Johnson (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.