Skip to content

Instantly share code, notes, and snippets.

@thomasantony
Created February 16, 2025 01:22
Show Gist options
  • Save thomasantony/ebd502eb58ca0876816b07aac53c6c1e to your computer and use it in GitHub Desktop.
Save thomasantony/ebd502eb58ca0876816b07aac53c6c1e to your computer and use it in GitHub Desktop.
Split text into sections
def split_tracks(input_file, output_pattern="Track{}.md"):
current_track = None
current_content = []
with open(input_file, 'r', encoding='utf-8') as f:
lines = f.readlines()
for line in lines:
line = line.strip()
# Check if line contains track marker
if line.lower().startswith('track ') and any(c.isdigit() for c in line):
# If we have content from previous track, save it
if current_track is not None:
with open(output_pattern.format(current_track), 'w', encoding='utf-8') as f:
f.write(f"# Track {current_track}\n")
f.write('\n'.join(current_content))
# Extract track number and start new collection
current_track = ''.join(c for c in line if c.isdigit())
current_content = []
elif current_track is not None:
current_content.append(line)
# Don't forget to save the last track
if current_track is not None and current_content:
with open(output_pattern.format(current_track), 'w', encoding='utf-8') as f:
f.write(f"# Track {current_track}\n")
f.write('\n'.join(current_content))
# Usage
split_tracks("FullText.txt")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment