Skip to content

Instantly share code, notes, and snippets.

@nottrobin
Created April 6, 2016 13:51
Show Gist options
  • Select an option

  • Save nottrobin/4a4f0162be577703cf851fbbe77f411b to your computer and use it in GitHub Desktop.

Select an option

Save nottrobin/4a4f0162be577703cf851fbbe77f411b to your computer and use it in GitHub Desktop.
Convert a file of doublespaced redirect instructions to JSON
#!/usr/bin/env python
"""
This will consume a 'redirects.txt' file containing redirects of the format:
request/path http://example.com/target/path # some comment
And output the same redirects in formatted JSON format instead:
{
"request/path": "http://example.com/target/path"
}
"""
import json
redirects = dict([
i.split(' ')[0:2] #ignore the 3rd item, in-line comments
for i in open('redirects.txt').readlines()
if len(i.split(' ')) >= 2 #ignore whole comment lines
])
print json.dumps(redirects, sort_keys=True, indent=4, separators=(',', ': '))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment