Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
104 views
in Technique[技术] by (71.8m points)

python - My JSON save and load function is not working

I am writing a simple function to save a twitter search as a JSON, and then load the results. The save function seems to work but the load one doesn't. The error I receive is:

"UnsupportedOperation: not readable"

Can you please advise what the issue might be in my script?

import io

def save_json(filename, data):
    with open('tweet2.json', 'w', encoding='utf8') as file:
        json.dump(data, file, ensure_ascii = False)

def load_json(filename):
    with open('tweet2.json', 'w', encoding = 'utf8') as file:
        return json.load(file)

#sample usage

q = 'Test'

results  = twitter_search(twitter_api, q, max_results = 10)

save_json = (q, results)
results = load_json(q)

print(json.dumps(results, indent = 1, ensure_ascii = False))
question from:https://stackoverflow.com/questions/65643229/my-json-save-and-load-function-is-not-working

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Using "w" you won't be able to read the file so you need to use "r" (Opens a file for reading only.)

open("tweet2.json","r")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...