2

This topic seems to be covered a lot but I have not been able to solve it using any of the solution offered by the suggestion in other people's posts. So here goes, I'm trying to write a .txt file after reading it from a url as follows:

from urllib.request import urlopen
fileobj = urlopen("http://www.gutenberg.org/ebooks/42671.txt.utf-8")
file = open('book.txt', 'w')
for byteseq in fileobj:
    line = byteseq.decode()
    file.write(line)
file.close()
fileobj.close()

However, I kept on getting UnicodeEncodeError: 'ascii' codec can't encode character on IDLE when I try to print the stripped line. The following is the code:

def function1():
    file = open('coba.txt', 'r')
    for line in file:
        print(line.strip())
    file.close()

Calling function1() gives an error message as follows:

File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/encodings/ascii.py", line 26, in decode
    return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xef in position 0: ordinal not in range(128)

The confusing bit is that, when I use terminal and execute the .py file using python3 file_name.py, it does print the line.strip(), and so does executing idle3 from terminal. However, opening idle3 from the App folder and using Sublime Text Python 3 build will result in the same error messages.

I am beyond clueless as to what is actually happening. Any help would be greatly appreciated!

sophros
  • 14,672
  • 11
  • 46
  • 75
kmugi
  • 343
  • 1
  • 4
  • 13
  • I'm not sure this is exactly your problem, so I won't flag as duplicate, but possibly [this](http://stackoverflow.com/q/10561923/3220135) would help – Aaron Oct 03 '16 at 15:33
  • When I do echo $LANG, it returns 'en_AU.UTF-8'. If I do python3 file_name.py from mac terminal, it works fine. If i launch it using idle3 from terminal, it works well too. It just doesn't work when I launch idle3 not from terminal and using the build from Sublime text 3 (I already have Python 3 build in it) – kmugi Oct 04 '16 at 00:30
  • Is it possible that your terminal supports UTF-8, so Python's print function hands the terminal the string as-is; however, Sublime Text's console does not support UTF-8, so Python's print function tries to convert the string to ascii? – mareoraft Feb 07 '17 at 20:30

0 Answers0