Skip to content

Instantly share code, notes, and snippets.

@henryscala
Created December 8, 2016 10:05
Show Gist options
  • Save henryscala/05618fc8531504a0f4b5cdfdd92dde70 to your computer and use it in GitHub Desktop.
Save henryscala/05618fc8531504a0f4b5cdfdd92dde70 to your computer and use it in GitHub Desktop.
a script to indent lines of text based on given start tag and end tag
# a script to indent lines of text based on given start tag and end tag
import os
import os.path
src_file = r"<sourcefile>"
dst_file = r"<destination-file>"
with open(src_file,"rb") as srcfile:
content = srcfile.read()
lines = content.splitlines()
START_TAG = "_START"
END_TAG = "_END"
INDENTATION = " "
num_indentation = 0
with open(dst_file,mode="wb") as file:
for line in lines:
#print("line:",line)
linestr = line.decode("utf-8")
if linestr.find(START_TAG) >= 0:
print("met start TAG")
linestr = INDENTATION*num_indentation + linestr
num_indentation += 1
elif linestr.find(END_TAG) >= 0:
print("met END TAG")
num_indentation -= 1
linestr = INDENTATION*num_indentation + linestr
else:
linestr = INDENTATION*num_indentation + linestr
file.write(bytearray(linestr,"utf-8"))
file.write(b"\n")
print("done.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment