将tweet保存在文件中,但它们会被Python覆盖

将tweet保存在文件中,但它们会被Python覆盖,python,twitter,Python,Twitter,我是编程新手。我创建了一个脚本,它循环遍历twitter id列表,检索它们的时间线,并将它们保存在json存档中。但是,我认为每次它都会占用最后一个时间线并覆盖文件,只保留最后一个时间线 这是我在循环中的代码: idfile = open('ids_part1.txt', 'r+') for id in idfile: if id: id = id.strip() try: result = ret_timeline(user_i

我是编程新手。我创建了一个脚本,它循环遍历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()

你能帮我吗?多谢各位

您需要在追加模式下打开文件,您应该可以

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

您需要在追加模式下打开文件,这样就可以了

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

您需要在追加模式下打开文件,这样就可以了

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

您需要在追加模式下打开文件,这样就可以了

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

w+
w
一样,会截断现有文件内容。阅读不需要一直打开文件的文档非常有用。只需在循环之前打开一次。
w+
w
一样,会截断现有文件内容。阅读不需要一直打开文件的文档非常有用。只需在循环之前打开一次。
w+
w
一样,会截断现有文件内容。阅读不需要一直打开文件的文档非常有用。只需在循环之前打开一次。
w+
w
一样,会截断现有文件内容。阅读不需要一直打开文件的文档非常有用。在循环之前打开它一次。