P
P
Program_Danil2018-07-23 10:11:11
Python
Program_Danil, 2018-07-23 10:11:11

What causes the program to crash when reading a text file?

When the file is read, a trace is output. Below is the error and the code that opens the file.
Error:
5b557c3939a26492087320.png
Code:

filenames = ['peter.txt', 'walkin_essays.txt']

for filename in filenames:
  with open(filename) as file_object:
    content = file_object.read()
    print(content.count('the'))

Error type translation:
UnicodeDecodeError: 'charmap' codec can't decode byte 0x98 in position 3589: character maps to : 'charmap' codec cannot
decode byte 0x98 at position 3589: character mapping <undefined>
Text files contain books, so their size is not small.
Maybe someone knows what it is?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
be52, 2018-07-23
@Program_Danil

file encoding is different or even different
with open(filename, encoding = 'utf8') try

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question