dotfiles

Author: Kyle Fuller <kyle@fuller.li> 2021-10-02 15:16:53 +0100 +0100
Committer: Kyle Fuller <kyle@fuller.li> 2021-10-02 15:16:53 +0100 +0100
Commit: 95b6194b85ba86f034a4300b1b0d46dc5ceea8d0
Parent: be869b6e340a25f79beaf5dd5542f8f41275925b


feat: add bin for converting csv to JSON
diff --git a/.local/bin/csv2json b/.local/bin/csv2json
new file mode 100755
index 0000000000000000000000000000000000000000..7abfb92ad632eb4f891a5d5aad4d07a340065200
--- /dev/null
+++ b/.local/bin/csv2json
@@ -0,0 +1,22 @@
+#!/usr/bin/env python3
+
+import json
+import sys
+from csv import DictReader
+from typing import Any, Dict, TextIO
+
+
+def csv2json(fp: TextIO) -> None:
+    reader = DictReader(fp)
+    for row in reader:
+        json.dump(row, sys.stdout)
+        print()
+
+
+if __name__ == '__main__':
+    for arg in sys.argv[1:]:
+        with open(arg) as fp:
+            csv2json(fp)
+
+    if len(sys.argv) == 1:
+        csv2json(sys.stdin)