]> git.phdru.name Git - dotfiles.git/blob - bin/iconv.py
Initial import
[dotfiles.git] / bin / iconv.py
1 #! /usr/bin/env python
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 if arguments:
19    for file in arguments:
20       infile = open(file)
21       try:
22          for line in infile:
23             sys.stdout.write(unicode(line, from_charset, "replace").encode(to_charset, "replace"))
24       except:
25          infile.close()
26 else:
27    for line in sys.stdin:
28       sys.stdout.write(unicode(line, from_charset, "replace").encode(to_charset, "replace"))