Strange characters in outout to file

I am trying to get the terminal output from python to a text file with one row at a time.
It’s working, but there are some characters in the output to the file which are not in the terminal:
a question mark in a square
Does anyone know what’s wrong here, tried to replace them with a space, but that’s not working either?

Screenshot is attached

import subprocess
import sys

log_file = open('log_file.txt', 'a', encoding="UTF-8")
p = subprocess.Popen('winget install --accept-source-agreements' ,stdout = subprocess.PIPE, stderr= subprocess.PIPE)    
# Poll process for new output until finished
while True:
    nextline = p.stdout.readline()
    if nextline == b'' and p.poll() != None:
    log_file.write(nextline.decode(sys.stdout.encoding, errors="ignore"))

output = p.communicate()[0]
exitCode = p.returncode

Schermafbeelding 2022-03-18 183535

Those questions marks look like control characters. Whenever a progress bar is written to the terminal, control characters are often used to tell the terminal how and when to reposition the cursor so that the progress bar looks pretty. These characters do not show up in the terminal output normally. However, if you write the terminal output to a file, those control characters end up in the file and become visible if you read the file.

See whether you can disable the progress bar. If not, try to figure out which control characters are being used and remove them.

thanks for the answer, couldn’t disable the progress bar. I have added a few if statements, that’s working, not the nicest way, but’s OK for now.

if b"Downloading" in nextline:
        log_file.write('Download started: ' + program_name + '\n')