0

I'm trying to print out a text file word by word, but it keeps giving me an error.

The text file is the bee movie script, so am I getting the error because the file is too big?

My code:

with open ('Script.txt', 'r') as file:
    for line in file:
        for word in line.split():
            print(word)

The error:

Traceback (most recent call last):
  File "C:/Users/itsan/Desktop/Coding/Python/Python Scripts/System/Bee Movie Script/main.py", line 2, in <module>
    for line in file:
  File "C:\Users\itsan\Desktop\Coding\Python\Python Install\lib\encodings\cp1252.py", line 23, in decode
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 252: character maps to <undefined>
itsanantk
  • 264
  • 1
  • 9
  • 1
    Probably because your file is encoded in a specific way, but your terminal doesn't support that encoding. try doing `open('script.txt', 'rb')` and do the same, and then you can play around with encodings via `print(word.decode('iso-8859-1'))` for instance. Or `open('script.txt', 'r', encoding="iso-8859-1")` to simplify things. – Torxed May 06 '20 at 19:07
  • console on Windows use encoding different then `utf-8` and it useually makes problem - but using Google you should find how to set encoding `utf-8` in Windows' console - it may need some changes in registers. – furas May 07 '20 at 07:18

0 Answers0