[Tutor] find pickle and retrieve saved data

Oscar Benjamin oscar.j.benjamin at gmail.com
Wed Aug 5 23:40:13 CEST 2015


On 4 August 2015 at 23:09, Quiles, Stephanie
<stephanie.quiles001 at albright.edu> wrote:
> I am still struggling with this one.

Hi Stephanie, Alan has already raised a few issues with your code so
I'm just going to address the one that's showing in your error
message.

These two lines are generating the error message:

>     infile = open("emails.dat", "r")
>     name = infile.readline()
>
> This is the error i am getting:
>
> enter a name in the file for info: sarah
> Traceback (most recent call last):
>   File "/Users/stephaniequiles/Downloads/findemails.py", line 28, in <module>
>     main()
>   File "/Users/stephaniequiles/Downloads/findemails.py", line 9, in main
>     name = infile.readline()
>   File "/Library/Frameworks/Python.framework/Versions/3.4/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 0x80 in position 0: invalid start byte

The interpreter is assuming that your file 'emails.dat' is a text file
using the utf-8 encoding. This is because you didn't specify an
encoding when opening the file. To specify that the encoding is e.g.
ascii you would do:

infile = open('emails.dat', 'r', encoding='ascii')

The error occurs because the bytes read from the file are not valid
for utf-8. What program did you use to write the emails.dat file? Does
it look reasonable when you open it in a text editor e.g. your code
editor or notepad?

If you show the output of the following command then someone may be
able to guess the encoding that you should be using for this file:

infile = open('emails.dat', 'rb')  # b for binary mode
print(repr(infile.read(100)))

--
Oscar


More information about the Tutor mailing list