43 lines
1.3 KiB
Python
43 lines
1.3 KiB
Python
#!/usr/bin/env python3
|
|
import sys
|
|
import re
|
|
|
|
def remove_line_comments(text):
|
|
"""
|
|
Entfernt aus einem String alles ab '//'.
|
|
Achtung: Naiver Ansatz, wenn '//' in Strings vorkommt,
|
|
werden sie fälschlich als Kommentar erkannt.
|
|
"""
|
|
# Regex: Entfernt alles von // bis Zeilenende
|
|
return re.sub(r'//.*', '', text)
|
|
|
|
def main():
|
|
if len(sys.argv) < 2 or len(sys.argv) > 3:
|
|
print("Usage: python remove_comments.py <input.json> [output.json]")
|
|
sys.exit(1)
|
|
|
|
input_file = sys.argv[1]
|
|
output_file = sys.argv[2] if len(sys.argv) == 3 else "cleaned.json"
|
|
|
|
# Lese die Eingabedatei zeilenweise
|
|
try:
|
|
with open(input_file, 'r', encoding='utf-8') as f:
|
|
lines = f.readlines()
|
|
except Exception as e:
|
|
print(f"Fehler beim Einlesen der Datei '{input_file}': {e}")
|
|
sys.exit(1)
|
|
|
|
# Entferne Kommentare aus jeder Zeile
|
|
cleaned_lines = [remove_line_comments(line) for line in lines]
|
|
|
|
# Schreibe das Ergebnis in die Ausgabedatei
|
|
try:
|
|
with open(output_file, 'w', encoding='utf-8') as f:
|
|
f.writelines(cleaned_lines)
|
|
print(f"Kommentare entfernt. Gespeicherte Datei: {output_file}")
|
|
except Exception as e:
|
|
print(f"Fehler beim Schreiben in die Datei '{output_file}': {e}")
|
|
sys.exit(1)
|
|
|
|
if __name__ == "__main__":
|
|
main() |