View difference between Paste ID: Z1SXv2yf and PcTrHQEv
SHOW: | | - or go back to the newest paste.
1-
#1/usr/bin/env python
1+
#1/usr/bin/env python
2-
2+
3-
# this is from The Python
3+
# this is from The Python
4-
# Standard Library by example
4+
# Standard Library by example
5-
# ISBN13: 9780321767349
5+
# ISBN13: 9780321767349
6-
6+
7-
7+
8-
from codecs_to_hex import to_hex
8+
from codecs_to_hex import to_hex
9-
9+
10-
import codecs
10+
import codecs
11-
import sys
11+
import sys
12-
12+
13-
encoding = sys.argv[1]
13+
encoding = sys.argv[1]
14-
filename = encoding + '.txt'
14+
filename = encoding + '.txt'
15-
15+
16-
print 'Writing to', filename
16+
print 'Writing to', filename
17-
with codecs.open(filename, mode='wt', encoding=encoding) as f:
17+
with codecs.open(filename, mode='wt', encoding=encoding) as f:
18-
    f.write(u'pi: \u03c0')
18+
    f.write(u'pi: \u03c0')
19-
19+
20-
# Determine the byte grouping to use for to_hex()
20+
# Determine the byte grouping to use for to_hex()
21-
nbytes = {'utf-8': 1,
21+
nbytes = {'utf-8': 1,
22-
          'utf-16': 2,
22+
          'utf-16': 2,
23-
          'utf-32': 4,
23+
          'utf-32': 4,
24-
          }.get(encoding, 1)
24+
          }.get(encoding, 1)
25-
25+
26-
# Show the raw bytes in the file
26+
# Show the raw bytes in the file
27-
print 'File contents:'
27+
print 'File contents:'
28-
with open(filename, mode='rt') as f:
28+
with open(filename, mode='rt') as f:
29-
    print to_hex(f.read(), nbytes)
29+
    print to_hex(f.read(), nbytes)
30-
30+
31-
""" Traceback (most recent call last):
31+
# usage $ python codecs_open_write.py utf-8
32-
    File "codecs_open_write.py", line 13, in <module>
32+
#	  python codecs_open_write.py utf-16
33-
    encoding = sys.argv[1]
33+
#	  python codecs_open_write.py utf-32