UnicodeDecode关于"utf-8"编解码器无法解码 Python 中的字节0x96错误



我得到一个UnicodeDecodeError关于'utf-8'编解码器无法解码字节0x96在位置68455。我通过添加编码行尝试了一些解决方案。还是没有运气。如何解决此错误?


def create_HTML(config):
in_doc = Path(config.doc)
if config.o:
out_file = Path(config.o)
else:
out_file = Path.cwd() / f'{in_doc.stem}_email.html'
template_file = config.t
# Read in the entire file as a list
# This can be problematic if the file is really large
with open(in_doc) as f:
all_content = f.readlines()
# Get the title line and clean it up
title_line = all_content[0]
title = f'My Newsletter - {title_line[7:].strip()}'
# Parse out the body from the meta data content at the top of the file
body_content = all_content[6:]
# Create a markdown object and convert the list of file lines to HTML
markdowner = Markdown()
markdown_content = markdowner.convert(''.join(body_content))
# Set up jinja templates
env = Environment(loader=FileSystemLoader('.'))
template = env.get_template(template_file)
# Define the template variables and render
template_vars = {'email_content': markdown_content, 'title': title}
raw_html = template.render(template_vars)
# Generate the final output string
# Inline all the CSS using premailer.transform
# Use BeautifulSoup to make the formatting nicer
soup = BeautifulSoup(transform(raw_html), 'html.parser').prettify(formatter="html")
# The unsubscribe tag gets mangled. Clean it up.
final_HTML = str(soup).replace('%7B%7BUnsubscribeURL%7D%7D', '{{UnsubscribeURL}}')
out_file.write_text(final_HTML)
File "C:appspython3.7.9libsite-packagesjinja2loaders.py", line 201, in get_source        
contents = f.read().decode(self.encoding)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x96 in position 68455: invalid start byte

0x96是二进制10010110,任何匹配模式10XXXXXX (0x80到0xBF)的字节只能是UTF-8编码中的第二个或后续字节。因此,流要么不是UTF-8,要么被损坏。由于没有说明编码,您应该尝试ISO-8859-1(又名"拉丁1")。