Python Forum

Full Version: for line in new:
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,
I try to write program who will find the word in the file.

new=open(r'C:\Users\andrz\Desktop\New folder (3)\New.docx')
for line in new:
    line=line.rstrip()
    if not 'But' in line:
        continue
    print(line)
I have this Error:
Traceback (most recent call last):
File "C:/Users/andrz/PycharmProjects/selenium/New.py", line 3, in <module>
for line in new:
File "C:\Users\andrz\AppData\Local\Programs\Python\Python38-32\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 0x81 in position 637: character maps to <undefined>

What should I do?
You should not try to read a *.docx file because it is no text file. It is zipped XML. So save the file New.docx as test ("Save as" and choose "text").
There are modules to handle *.docx files, but I don't believe that is what you need now.