Skip to content

Instantly share code, notes, and snippets.

@mekkanizer
Created August 1, 2017 04:48
Show Gist options
  • Save mekkanizer/c97e098b6f61af8e8376b1974378dab3 to your computer and use it in GitHub Desktop.
Save mekkanizer/c97e098b6f61af8e8376b1974378dab3 to your computer and use it in GitHub Desktop.
the re.sub 'JS dict => valid JSON' transition
with open('/home/mekkanizer/proj/chili/feature.json') as data_file:
string = data_file.read().replace('\n', '')
string = string.replace(r"\'",'`')
string = re.sub(r"keywords: '.*?,\W+","",string)
pattern = re.compile(r"([0-9]|description|notes|title): '(.*?)'")
match = pattern.search(string) # fucking 's all over the place
while match is not None:
s = match.group(1) + ': "' + match.group(2).translate(str.maketrans({'"': r'\"'})) + '"'#,"\'":"'"
string = string.replace(match.group(),s)
match = pattern.search(string,match.end())
string = re.sub(r' {4}(\w+):', r' "\1":', string) #word:
string = re.sub(r': (\.[0-9]+)', r': 0\1', string) #2: .714673
string = re.sub(r'\[\"(\w+)\"\]', r'"\1"', string) #["PNG"]
string = re.sub(r'\!([0-9]+)', r'\1', string) #!2
data = json.loads(string)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment