I uploaded data in a json file as below
import json
characters = ['Robin Hood', 'Aladdin', 'Muhammad Ali', 'Santa Claus', 'Che Guevara']
filename = 'username.json'
with open(filename, 'w') as f:
json.dump(characters, f)
I was told not to use the read() method to read the file but instead json's load() method. The thing is when I try the both I end up with the same terminal output.
# same file
with open(filename) as f:
characters = json.load(f)
print('Using load():', characters)
# same file
with open(filename) as f:
characters = f.read()
print('Using read():', characters)
output
Using load(): ['Robin Hood', 'Aladdin', 'Muhammad Ali', 'Santa Claus', 'Che Guevara']
Using read(): ["Robin Hood", "Aladdin", "Muhammad Ali", "Santa Claus", "Che Guevara"]
Is there any difference?
"). And no, they are not the same thing. The former is the "string representation of a list" while the latter is simply a string.type(characters). Then you'll see the differenceread(), notjson.load(). Also my point was you should always post the actual output; not the output you think you're seeing.