首页 > 解决方案 > 以 UTF-8 写入 txt 文件 - Python

问题描述

标签: pythondjangopython-3.xdjango-viewstext-processing

解决方案


The issue here is that you're calling open() on a file without specifying the encoding. As noted in the Python documentation, the default encoding is platform dependent. That's probably why you're seeing different results in Windows and MacOS.

Assuming that the file itself was actually encoded in UTF-8, just specify that when reading the file:

original = open(str(last_uploaded.document), 'r', encoding="utf-8")

推荐阅读