Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #1/usr/bin/env python
- # this is from The Python
- # Standard Library by example
- # ISBN13: 9780321767349
- from codecs_to_hex import to_hex
- import codecs
- import sys
- encoding = sys.argv[1]
- filename = encoding + '.txt'
- print 'Writing to', filename
- with codecs.open(filename, mode='wt', encoding=encoding) as f:
- f.write(u'pi: \u03c0')
- # Determine the byte grouping to use for to_hex()
- nbytes = {'utf-8': 1,
- 'utf-16': 2,
- 'utf-32': 4,
- }.get(encoding, 1)
- # Show the raw bytes in the file
- print 'File contents:'
- with open(filename, mode='rt') as f:
- print to_hex(f.read(), nbytes)
- """ Traceback (most recent call last):
- File "codecs_open_write.py", line 13, in <module>
- encoding = sys.argv[1]
- IndexError: list index out of range
- """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement