Skip to content

Instantly share code, notes, and snippets.

@CSaratakij
Last active February 28, 2023 22:35
Show Gist options
  • Save CSaratakij/d2831fc8818b2276e2decb68a9764622 to your computer and use it in GitHub Desktop.
Save CSaratakij/d2831fc8818b2276e2decb68a9764622 to your computer and use it in GitHub Desktop.
Json comment remover '//'
#!/usr/bin/env python3
import argparse
from pathlib import Path
commentSymbol = "//"
parser = argparse.ArgumentParser(description="Remove comment from json.")
parser.add_argument("input", help="input file path")
parser.add_argument("output", help="output file path")
args = parser.parse_args()
if args.input == args.output:
print("Error : output file must not be the same name as input file...")
else:
try:
inputFile = open(args.input, mode='r', encoding='utf-8')
outputFilePath = Path(args.output)
outputFilePath.touch(exist_ok=True)
outputFile = open(outputFilePath, 'w', encoding='utf-8')
for line in inputFile:
if commentSymbol in line:
continue
if not line.isspace():
outputFile.write(line)
finally:
inputFile.close()
outputFile.close()
python remover.py input.json output.json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment