From 64f38cb5c76f8eacad12204f4699a1173e44556e Mon Sep 17 00:00:00 2001 From: Czechman Date: Sun, 16 Feb 2025 17:59:23 +0100 Subject: [PATCH] display_json angelegt --- display_json.py | 61 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 display_json.py diff --git a/display_json.py b/display_json.py new file mode 100644 index 0000000..f1694f5 --- /dev/null +++ b/display_json.py @@ -0,0 +1,61 @@ +#!/usr/bin/env python3 +import json +import argparse + +def pretty_print_json(data): + """ + Gibt die JSON-Daten als schön formatierten String zurück. + """ + return json.dumps(data, indent=2, ensure_ascii=False) + +def write_html_file(pretty_json_str, output_file): + """ + Schreibt den formatierten JSON-String in eine HTML-Datei, + wobei er in einem
-Block angezeigt wird.
+    """
+    html_content = f"""
+
+
+  
+  JSON Display
+  
+
+
+{pretty_json_str}
+
+"""
+    with open(output_file, "w", encoding="utf-8") as f:
+        f.write(html_content)
+
+def main():
+    parser = argparse.ArgumentParser(
+        description="Lädt eine JSON-Datei, zeigt sie in der Konsole an und speichert sie als HTML."
+    )
+    parser.add_argument("input", help="Pfad zur Eingabe-JSON-Datei.")
+    parser.add_argument("-o", "--output", default="output.html",
+                        help="Pfad zur Ausgabe-HTML-Datei (Standard: output.html).")
+    args = parser.parse_args()
+
+    # JSON-Datei laden
+    with open(args.input, "r", encoding="utf-8") as f:
+        data = json.load(f)
+
+    # JSON schön formatieren
+    pretty_json_str = pretty_print_json(data)
+
+    # In der Konsole ausgeben
+    print(pretty_json_str)
+
+    # In HTML-Datei schreiben
+    write_html_file(pretty_json_str, args.output)
+    print(f"\nHTML-Ausgabe gespeichert in '{args.output}'.")
+
+if __name__ == "__main__":
+    main()