0
text_file = open("university_towns.txt")
State = {idx: lines.decode('utf-8').strip().replace("[edit]", "") 
             for idx,lines in enumerate(text_file) if "edit" in lines}

My code raised an error:

'ascii' codec can't decode byte 0xe2 in position 6723    

I am using Python 3.5.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • Python 2 or Python 3? – Arya McCarthy May 19 '17 at 04:47
  • Looks like byte 6723 is not proper unicode. Passing `errors='ignore'` to ` decode()` might help. – Klaus D. May 19 '17 at 04:49
  • 1
    In python 3.x, since you didn't specify an encoding the file was opened in whatever mode `sys.getdefaultencoding()` says. `lines.decode('utf-8')` is invalid because 3.x strings don't have a `decode` method so you didn't get that far or you'd have a different error. My guess is that the problem is in `enumerate(text_file)`... but that would mean your default encoding is `ascii`. You could just read the file line by line and see if you get the error just doing that. – tdelaney May 19 '17 at 05:00

1 Answers1

1

Remove decode("utf-8) from second line and it might solve your problem!

As in python 2 we make use of encode and decode for related problems. But in python 3.x , str/bytes make similar use of that.

You nay read the following reference:

python-3-encode-decode-vs-bytes-str

Hope, it helps!

Community
  • 1
  • 1
abhinav
  • 1,108
  • 11
  • 23