【问题标题】:Saving tweets in a file, but they are overwritten by Python将推文保存在文件中,但它们被 Python 覆盖
【发布时间】:2015-10-07 04:25:01
【问题描述】:

我是编程新手。我创建了一个脚本,它遍历 twitter id 列表,检索它们的时间线并将它们保存在 json 存档中。但是,我认为每次它需要最后一个时间线并覆盖文件并只保留最后一个。

这是我在循环中的代码:

idfile = open('ids_part1.txt', 'r+')
for id in idfile:
    if id:
        id = id.strip()
        try:
            result = ret_timeline(user_id=id)
            if result:
                result_file = open('result1.json', 'w+')
                for st in result:
                    result_file.write(json.dumps(st._json, indent=4, sort_keys=True))
                result_file.close()

你能帮我解决这个问题吗?非常感谢!

【问题讨论】:

标签: python twitter


【解决方案1】:

你需要以追加模式打开文件,你应该没问题

result_file = open('result1.json', 'a')

【讨论】:

    猜你喜欢
    • 2020-06-25
    • 1970-01-01
    • 2011-09-07
    • 2019-02-20
    • 2014-02-22
    • 1970-01-01
    • 1970-01-01
    • 2018-06-17
    • 1970-01-01
    相关资源
    最近更新 更多