unicode怎么办?

What to do about unicode?

我有以下代码从 xml 个文件中剥离白线:

#working for all files in dir.
from pathlib import Path


for path in Path(r'attachments090058\Status\XMLsend').glob('*.xml'):
    with open(path, 'r+') as f:
        lines = f.readlines()
        f.seek(0, 0)
        for line in lines:
            if line.strip() != '':
                f.write(line)
        f.truncate()

它工作得很好,但是现在我收到以下错误:

UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 1291: character maps to <undefined>

怎么办?

请帮忙!

尝试将文本编码为 UTF-8 format

试试这个代码:

from pathlib import Path

for path in Path(r'attachments090058\Status\XMLsend').glob('*.xml'):
    with open(path, 'r+', encoding='utf-8') as f:
        lines = f.readlines()
        f.seek(0, 0)
        for line in lines:
            if line.strip() != '':
                f.write(line)
        f.truncate()