Last active
August 29, 2015 14:02
-
-
Save LiamInJapan/e4d5f30046aa91e9d007 to your computer and use it in GitHub Desktop.
Python script to parse a file of text into javascript multiline string format to allow easy inlining in JS code. E.g. when you want to inline a block of JSON in JS code to keep things local
This file contains 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
from optparse import OptionParser | |
def main(): | |
parser = OptionParser(usage="usage: %prog [options] filename", | |
version="%prog 1.0") | |
parser.add_option("-f", "--file", | |
action="store", | |
dest="filename", | |
help="file to parse",) | |
(options, args) = parser.parse_args() | |
if len(args) != 1: | |
parser.error("wrong number of arguments") | |
f = open(args[0],'r') | |
string = "" | |
while 1: | |
line = f.readline() | |
if not line:break | |
string += line | |
f.close() | |
print string | |
print "" | |
print "---------" | |
string = string.replace("\"", "\\\"") | |
string = string.replace("\n", "\\\n") | |
string = "\"" + string + "\"" | |
print string | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment