Skip to content
TheChatArchiveThe Chat Archive
o1-preview-2024-09-12· OpenAIAi2 WildChat-4.8M (AI2)

import json Открываем исходный JSON-файл по указанному пути with…

  1. User

    import json

    Открываем исходный JSON-файл по указанному пути

    with open(r'C:\Users\Admin\Downloads\AyuGram Desktop\ChatExport_2024-09-22\result.json', 'r', encoding='utf-8') as infile: data = json.load(infile)

    cleaned_messages = []

    Проходим по всем сообщениям

    for message in data['messages']: # Отбираем только нужные поля cleaned_message = { 'id': message.get('id'), 'date': message.get('date'), 'from': message.get('from'), 'text': '' } # Обрабатываем поле 'text', если оно содержит список или строку text_content = message.get('text', '') if isinstance(text_content, list): # Если текст представлен списком (с вложенными структурами) text_lines = [] for item in text_content: if isinstance(item, str): text_lines.append(item) elif isinstance(item, dict) and 'text' in item: text_lines.append(item['text']) cleaned_message['text'] = ''.join(text_lines) elif isinstance(text_content, str): cleaned_message['text'] = text_content else: cleaned_message['text'] = ''

    # Добавляем очищенное сообщение в список
    cleaned_messages.append(cleaned_message)
    

    Сохраняем очищенные данные в новый файл

    with open(r'C:\Users\Admin\Downloads\AyuGram Desktop\ChatExport_2024-09-22\cleaned_result.json', 'w', encoding='utf-8') as outfile: json.dump({'messages': cleaned_messages}, outfile, ensure_ascii=False, indent=2)

  2. Assistant

    Could you please provide more details on how I can assist you with this code?