Reputation: 23
import json
f = open("Troubleshooting.txt","a")
json.dump(problem,f)
f.close()
I've tried using json but it keeps writing the list on a single line which makes it extremely messy. For example - json.dump(problem) Writes in the txt file: ["Phone has gotten wet", "The display is broken", "The phone does not charge", "There is no sound", "The interface is slow", "Nothing is saving"]
Then when the script is restarted with different values: json.dump(problem) Simply adds on this to the previous list: ["The phone doesn't turn on", "The phone does not charge"]
Making it all together be one line saying: ["Phone has gotten wet", "The display is broken", "The phone does not charge", "There is no sound", "The interface is slow", "Nothing is saving"]["The phone doesn't turn on", "The phone does not charge"]
Is there any way to make the other parts be written on a new line?
Upvotes: 0
Views: 1470
Reputation: 9969
You can manually add newlines, like so
f = open("Troubleshooting.txt","a")
f.write('\n')
json.dump(problem,f)
f.close()
But also you don't necessarily even need to use JSON, if you just want strings you could iterate over the list and write each as a new line.
f = open("Troubleshooting.txt","a")
for line in problem:
f.write(line + '\n')
f.close()
Upvotes: 0
Reputation: 3209
You can use f.write('\n')
to add a new line to the file.
I.E. make your code into:
import json
f = open("Troubleshooting.txt","a")
f.write('\n')
json.dump(problem,f)
f.close()
Upvotes: 2