Skip to content

Instantly share code, notes, and snippets.

@anadim
Created November 7, 2023 02:39
Show Gist options
  • Save anadim/f6e9b597fc146dda88b3efe7792db8a9 to your computer and use it in GitHub Desktop.
Save anadim/f6e9b597fc146dda88b3efe7792db8a9 to your computer and use it in GitHub Desktop.
import glob
import os
import json
# Get the current working directory
current_directory = os.getcwd()
# Define the pattern for the JSON files we are interested in
file_pattern = os.path.join(current_directory, '2023-*.json')
# Function to extract message texts from a JSON file
def extract_messages(json_file_path):
with open(json_file_path, 'r') as file:
json_data = json.load(file)
return [message['text'] for message in json_data if 'text' in message]
# Find all matching JSON files in the current directory
json_files = glob.glob(file_pattern)
# Dictionary to hold file names and their messages
messages_per_file = {}
# Loop through the files and extract messages
for json_file in json_files:
messages = extract_messages(json_file)
if messages: # Only add to the dictionary if messages were found
messages_per_file[os.path.basename(json_file)] = messages
# Define the name of the output text file
output_file_path = os.path.join(current_directory, 'extracted_messages.txt')
# Check if we found any messages and write them to the text file
if messages_per_file:
with open(output_file_path, 'w') as output_file:
for file_name, messages in messages_per_file.items():
output_file.write(f'Messages from {file_name}:\n')
for message in messages:
output_file.write(message + '\n')
output_file.write('\n') # Add an extra newline for separation between files
print(f"Messages have been written to {output_file_path}")
else:
print("No messages found in the files.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment