【问题标题】:My JSON save and load function is not working我的 JSON 保存和加载功能不起作用
【发布时间】:2021-04-15 00:01:16
【问题描述】:

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

“UnsupportedOperation:不可读”

您能否告知我的脚本中可能存在什么问题?

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

【问题讨论】:

    标签: python json twitter


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-10-20
      • 1970-01-01
      • 2023-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多