em4995
em4995

Reputation: 77

UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 295: character maps to <undefined>

I am trying to run this program from a book. I have created the file called 'alice_2.txt'

def count_words(filename):
    """Count the approximate number of words in a file."""
    try:
        with open(filename) as f_obj: 
            contents = f_obj.read()
    except FileNotFoundError:
        msg = "Sorry, the file " + filename + " does not exist."
        print(msg)
    else:
        # Count approximate number of words in the file.
        words = contents.split()
        num_words = len(words)
        print("The file " + filename + " has about " + str(num_words) +
            " words.")
            
filename = 'alice_2.txt'
count_words(filename)

But I keep getting this error message

UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 295: character maps to <undefined>

Can anyone explain what this means, and how to solve it?

Upvotes: 0

Views: 1017

Answers (1)

Equinox
Equinox

Reputation: 6758

You are trying to use an encoding which cannot store the character you have in file. for example ɛ can't be opened in ascii since it doesn't have valid ascii code.

try to open the file using utf-8.

with open(filename, encoding='utf8') as f_obj:
    pass
    # DO your stuff

Upvotes: 1

Related Questions