Read special characters from .txt file in python

前端 未结 3 501
滥情空心
滥情空心 2021-01-19 21:56

The goal of this code is to find the frequency of words used in a book.

I am tying to read in the text of a book but the following line keeps throwing my code off:

3条回答
  •  南笙
    南笙 (楼主)
    2021-01-19 22:06

    When you open a text file in python, the encoding is ANSI by default, so it doesn't contain your é chartecter. Try

    word_file = open ("./words.txt", "r", encoding='utf-8')
    

提交回复
热议问题