Skip to content

Instantly share code, notes, and snippets.

@adamjtaylor
Last active November 12, 2021 14:37
Show Gist options
  • Save adamjtaylor/bbadf5aa4beef9aa1d1a50d76e2c5bec to your computer and use it in GitHub Desktop.
Save adamjtaylor/bbadf5aa4beef9aa1d1a50d76e2c5bec to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import argparse
import re
from shutil import copy
import os
import json
parser = argparse.ArgumentParser()
parser.add_argument('input',
type=str,
help='the file to parse the headers of ')
args = parser.parse_args()
with open(args.input, 'r') as f:
input=f.read()
pattern = r'(Groups.+Name\"\:\ \")(.*?)(\"\,.*?,)(.*?\]\}\])'
to_insert = ' "Colors": ["6000a0","d030d0"], "Channels": ["Hematoxylin","Eosin"]}]'
replacement_string = '\\1' + 'H&E' + '\\3' + to_insert
fix = re.sub(pattern, replacement_string, str(input))
newname = args.input + '.bak'
copy(args.input, newname)
with open(args.input, 'w', encoding='utf-8') as f:
f.write(fix)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment