Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
for line in new:
#1
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?
Reply
#2
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.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020