Created
April 6, 2016 13:51
-
-
Save nottrobin/4a4f0162be577703cf851fbbe77f411b to your computer and use it in GitHub Desktop.
Convert a file of doublespaced redirect instructions to JSON
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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