* Added eucjp_to_utf8.py transcoder script.
authorUrban Wallasch <urban.wallasch@freenet.de>
Fri, 18 Jun 2021 15:36:14 +0000 (17:36 +0200)
committerUrban Wallasch <urban.wallasch@freenet.de>
Fri, 18 Jun 2021 15:36:14 +0000 (17:36 +0200)
README.md
eucjp_to_utf8.py [new file with mode: 0755]

index c8b7828c350ee159e015889e2156a1c061bcbb6e..6206ac9fc4f357f4f46725f2d85ff358c729744a 100644 (file)
--- a/README.md
+++ b/README.md
@@ -44,6 +44,10 @@ particular indicate whether a file is actually in EDICT format. In many
 cases a conversion from EUC-JP to UTF-8 will be necessary, as outlined in
 the examples above.
 
+**HINT:** In case the `recode` utility is not available, the included
+transcoding script may be used instead, e.g.:
+> `./eucjp_to_utf8.py enamdict.gz enamdict`
+
 
 ## Notes
 
diff --git a/eucjp_to_utf8.py b/eucjp_to_utf8.py
new file mode 100755 (executable)
index 0000000..bcfbbb1
--- /dev/null
@@ -0,0 +1,30 @@
+#!/usr/bin/python3
+
+"""
+    eucjp_to_utf8.py
+
+    Convert EUC-JP encoded text to UTF-8.
+    Input files with .gz extension are automatically decompressed.
+
+    USAGE:  eucjp_to_utf8.py [infile [outfile]]
+
+    Copyright (c) 2021 Urban Wallasch <irrwahn35@freenet.de>
+    Modified ("3-clause") BSD License
+"""
+
+import sys
+import codecs
+
+if len(sys.argv) > 1:
+    iname = sys.argv[1]
+    ifile = open(iname, 'rb')
+    if iname[-3:] == '.gz':
+        import gzip
+        ifile = gzip.GzipFile(fileobj=ifile)
+else:
+    ifile = sys.stdin.detach()
+
+ofile = open(sys.argv[2], 'w') if len(sys.argv) > 2 else sys.stdout
+
+for line in ifile:
+    print(line.decode('euc_jp'), end='', file=ofile)