]> git.phdru.name Git - dotfiles.git/blob - bin/iconv.py
9cb164fef0aa72bc6a2399d1ab4a988dd27d4c59
[dotfiles.git] / bin / iconv.py
1 #! /usr/bin/env python3
2 "Recode to default charset"
3
4 import sys
5 from getopt import getopt
6 from m_lib.defenc import default_encoding
7
8 from_charset = "cp1251"
9 to_charset = default_encoding
10
11 options, arguments = getopt(sys.argv[1:], 'f:t:')
12 for option, value in options:
13     if option == '-f':
14         from_charset = value
15     elif option == '-t':
16         to_charset = value
17
18 output = getattr(sys.stdout, 'buffer', sys.stdout)
19
20
21 if arguments:
22     for file in arguments:
23         with open(file, 'rb') as infile:
24             for line in infile:
25                 output.write(
26                     line.decode(from_charset, "replace").
27                     encode(to_charset, "replace"))
28 else:
29     input = getattr(sys.stdin, 'buffer', sys.stdin)
30     for line in input:
31         output.write(
32             line.decode(from_charset, "replace").encode(to_charset, "replace"))