Home > other >  Printing only one part when open file with "rb" (python 3.0)
Printing only one part when open file with "rb" (python 3.0)

Time:10-16

could someone explain to me why its printing only one part of the text? I would like it to print all the parts. Many thanks

import json
import os
import pickle

import requests

dd={'total_results': 27, 'messages': [[{'id': '1030546118733480126', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:22:21.327000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}], [{'id': '1030546117412270110', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:22:21.012000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}], [{'id': '1030546116321742909', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:22:20.752000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}], [{'id': '1030546115021516871', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:22:20.442000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}], [{'id': '1030546113469628476', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:22:20.072000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}], [{'id': '1030546110856560660', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:22:19.449000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}], [{'id': '1030546109686358026', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:22:19.170000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}], [{'id': '1030546083950104616', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:22:13.034000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}], [{'id': '1030546082415005798', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:22:12.668000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}], [{'id': '1030546081458692187', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:22:12.440000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}], [{'id': '1030546079739039766', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:22:12.030000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}], [{'id': '1030546073900560384', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:22:10.638000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}], [{'id': '1030546072801661050', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:22:10.376000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}], [{'id': '1030546071404945510', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:22:10.043000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}], [{'id': '1030546068418609222', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:22:09.331000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}], [{'id': '1030546066933825647', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:22:08.977000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}], [{'id': '1030546008268099666', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:21:54.990000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}], [{'id': '1030546006875582475', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:21:54.658000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}], [{'id': '1030546004908445817', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:21:54.189000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}], [{'id': '1030546004027645952', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:21:53.979000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}], [{'id': '1030546002698047519', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:21:53.662000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}], [{'id': '1030546000596705320', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:21:53.161000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}], [{'id': '1030545996985401487', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:21:52.300000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}], [{'id': '1030545672262398053', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:20:34.880000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}], [{'id': '1030545670291062806', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:20:34.410000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}]], 'analytics_id': '896a9309b64eb1512999a3958eecf364'}

search = json.dumps(dd, indent=4)
# print(search)
text = json.loads(search)
messages = text["messages"]
try:
    os.remove("text.txt")
except:
    pass
    
    
number = 0
for value in messages:
    x = (messages[number])

    with open("text.txt", "ab") as f:
        pickle.dump(x, f)
    number  = 1


with open("text.txt", "rb") as f:
    file = pickle.load(f)
print(file)

this is my result:

[{'id': '1030546118733480126', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:22:21.327000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}]

And i want this like this:


[{'id': '1030546118733480126', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:22:21.327000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}]
[{'id': '1030546117412270110', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:22:21.012000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}]
[{'id': '1030546116321742909', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:22:20.752000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}]
[{'id': '1030546115021516871', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:22:20.442000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}]
[{'id': '1030546113469628476', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:22:20.072000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}]
[{'id': '1030546110856560660', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:22:19.449000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}]
[{'id': '1030546109686358026', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:22:19.170000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}]
[{'id': '1030546083950104616', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:22:13.034000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}]
[{'id': '1030546082415005798', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:22:12.668000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}]
[{'id': '1030546081458692187', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:22:12.440000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}]
[{'id': '1030546079739039766', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:22:12.030000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}]
[{'id': '1030546073900560384', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:22:10.638000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}]
[{'id': '1030546072801661050', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:22:10.376000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}]
[{'id': '1030546071404945510', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:22:10.043000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}]
[{'id': '1030546068418609222', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:22:09.331000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}]
[{'id': '1030546066933825647', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:22:08.977000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}]
[{'id': '1030546008268099666', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:21:54.990000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}]
[{'id': '1030546006875582475', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:21:54.658000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}]
[{'id': '1030546004908445817', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:21:54.189000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}]
[{'id': '1030546004027645952', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:21:53.979000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}]
[{'id': '1030546002698047519', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:21:53.662000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}]
[{'id': '1030546000596705320', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:21:53.161000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}]
[{'id': '1030545996985401487', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:21:52.300000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}]
[{'id': '1030545672262398053', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:20:34.880000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}]
[{'id': '1030545670291062806', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:20:34.410000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}]

CodePudding user response:

import pickle



try:
    os.remove("text.txt")
except:
    pass

dd={'total_results': 27, 'messages': [[{'id': '1030546118733480126', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:22:21.327000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}], [{'id': '1030546117412270110', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:22:21.012000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}], [{'id': '1030546116321742909', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:22:20.752000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}], [{'id': '1030546115021516871', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:22:20.442000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}], [{'id': '1030546113469628476', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:22:20.072000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}], [{'id': '1030546110856560660', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:22:19.449000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}], [{'id': '1030546109686358026', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:22:19.170000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}], [{'id': '1030546083950104616', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:22:13.034000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}], [{'id': '1030546082415005798', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:22:12.668000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}], [{'id': '1030546081458692187', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:22:12.440000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}], [{'id': '1030546079739039766', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:22:12.030000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}], [{'id': '1030546073900560384', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:22:10.638000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}], [{'id': '1030546072801661050', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:22:10.376000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}], [{'id': '1030546071404945510', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:22:10.043000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}], [{'id': '1030546068418609222', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:22:09.331000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}], [{'id': '1030546066933825647', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:22:08.977000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}], [{'id': '1030546008268099666', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:21:54.990000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}], [{'id': '1030546006875582475', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:21:54.658000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}], [{'id': '1030546004908445817', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:21:54.189000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}], [{'id': '1030546004027645952', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:21:53.979000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}], [{'id': '1030546002698047519', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:21:53.662000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}], [{'id': '1030546000596705320', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:21:53.161000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}], [{'id': '1030545996985401487', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:21:52.300000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}], [{'id': '1030545672262398053', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:20:34.880000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}], [{'id': '1030545670291062806', 'type': 0, 'content': 's', 'channel_id': '995640046583562324', 'author': {'id': '644215898122616855', 'username': 'tomascz122', 'avatar': None, 'avatar_decoration': None, 'discriminator': '2668', 'public_flags': 0}, 'attachments': [], 'embeds': [], 'mentions': [], 'mention_roles': [], 'pinned': False, 'mention_everyone': False, 'tts': False, 'timestamp': '2022-10-14T18:20:34.410000 00:00', 'edited_timestamp': None, 'flags': 0, 'components': [], 'hit': True}]], 'analytics_id': '896a9309b64eb1512999a3958eecf364'}

messages = dd["messages"]

with open("text.txt", "ab") as f:
     pickle.dump(messages, f)



with open("text.txt", "rb") as f:
    messages = pickle.load(f)
for message in messages:
      print(message)

CodePudding user response:

Since the file is opened with the 'a' attribute, multiple objects can be appended in the same file.

You must repeat the call to pickle.load() for each object.

Here is one example, in which the first pickled object is an integer specifying the number of objects that follow.

import pickle

FILE_NAME = 'test.bin'

OBJECTS = ( 1234, 3.14, "Abcde", (1,2,3,'XYZ') )

with open(FILE_NAME,'ab') as f:
  pickle.dump(len(OBJECTS), f)  # number of objects that follow
  for x in OBJECTS:  
    pickle.dump(x,f)

with open(FILE_NAME,'rb') as f:
  n = pickle.load(f)
  for j in range(n):
    x = pickle.load(f)
    print(x)

Although it still works, do not use a *.txt extension for a binary file, it may be confusing.

NOTE: see better answer by https://stackoverflow.com/users/19661530/islam-abdelmoumen, where the whole object is saved with one call to pickle.dump(). This way, only one call to pickle.load() is necessary. My answer, while not the optimal one, shows you where your problem is.

  • Related