我的JSON保存和加载函数不起作用



我正在编写一个简单的函数,将twitter搜索保存为JSON,然后加载结果。保存功能似乎有效,但加载功能无效。我收到的错误是:

"不支持的操作:不可读";

你能告诉我剧本中可能有什么问题吗?

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))

使用"w"将无法读取文件,因此需要使用"r"(打开一个只读文件。(

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

最新更新