python 3.x - How do I print non-ASCII characters in a file using python3? -


here's example of code. simple you'll see. when use print file ubuntu terminal window, following error message:

traceback (most recent call last):   file "/ascii_cat", line 22, in <module>     print_file_in_ascii(f)   file "/ascii_cat", line 16, in print_file_in_ascii     line in f:   file "/usr/lib/python3.4/codecs.py", line 319, in decode     (result, consumed) = self._buffer_decode(data, self.errors, final) unicodedecodeerror: 'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte 

code:

#!/usr/bin/python3  import sys  def contains_only_ascii(a_string):     try:         a_char in a_string.strip():             if ord(a_char) < 32 or ord(a_char) > 126:                 return false     except:         pass     return true  def print_file_in_ascii(fname):     open(fname, "r") f:         line in f:             if contains_only_ascii(line) == true:                 print(line, end="")  # sys.argv may multiple files when * using filename; globbing f in sys.argv[1:]:     print_file_in_ascii(f) 

you've opened file default encoding, on system utf-8. file not encoded in utf-8, reading file produces exception.

open file in correct encoding specifying encoding= parameter explicitly:

with open(fname,encoding='whatever_the_encoding_really_is') f: 

Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -