UTF-8 未正确保存 txt 文件 - Python
UTF-8 is not saving txt file correctly - Python
我的 django 应用程序读取一个文件,并将一些报告保存在其他 txt 文件中。除了我的语言字母,一切正常。我的意思是,encoding="utf-8"
无法阅读某些字母。这是我的代码和报告文件示例:
views.py
:
def result(request):
#region variables
# Gets the last uploaded document
last_uploaded = OriginalDocument.objects.latest('id')
# Opens the las uploaded document
original = open(str(last_uploaded.document), 'r')
# Reads the last uploaded document after opening it
original_words = original.read().lower().split()
# Shows up number of WORDS in document
words_count = len(original_words)
# Opens the original document, reads it, and returns number of characters
open_original = open(str(last_uploaded.document), "r")
read_original = open_original.read()
characters_count = len(read_original)
# Makes report about result
report_fives = open("static/report_documents/" + str(last_uploaded.student_name) +
"-" + str(last_uploaded.document_title) + "-5.txt", 'w', encoding="utf-8")
report_twenties = open("static/report_documents/" + str(last_uploaded.student_name) +
"-" + str(last_uploaded.document_title) + "-20.txt", 'w', encoding="utf-8")
# Path to the documents with which original doc is comparing
path = 'static/other_documents/doc*.txt'
files = glob.glob(path)
#endregion
rows, found_count, fives_count, rounded_percentage_five, percentage_for_chart_five, fives_for_report, founded_docs_for_report = search_by_five(last_uploaded, 5, original_words, report_fives, files)
context = {
...
}
return render(request, 'result.html', context)
report file
:
['universitetindé™', 'té™hsili', 'alä±ram.', 'mé™n'] was found in static/other_documents\doc1.txt.
...
因为WindowsOS。在 Mac 上试用您的应用程序OS 它将毫无问题地运行)
我的 django 应用程序读取一个文件,并将一些报告保存在其他 txt 文件中。除了我的语言字母,一切正常。我的意思是,encoding="utf-8"
无法阅读某些字母。这是我的代码和报告文件示例:
views.py
:
def result(request):
#region variables
# Gets the last uploaded document
last_uploaded = OriginalDocument.objects.latest('id')
# Opens the las uploaded document
original = open(str(last_uploaded.document), 'r')
# Reads the last uploaded document after opening it
original_words = original.read().lower().split()
# Shows up number of WORDS in document
words_count = len(original_words)
# Opens the original document, reads it, and returns number of characters
open_original = open(str(last_uploaded.document), "r")
read_original = open_original.read()
characters_count = len(read_original)
# Makes report about result
report_fives = open("static/report_documents/" + str(last_uploaded.student_name) +
"-" + str(last_uploaded.document_title) + "-5.txt", 'w', encoding="utf-8")
report_twenties = open("static/report_documents/" + str(last_uploaded.student_name) +
"-" + str(last_uploaded.document_title) + "-20.txt", 'w', encoding="utf-8")
# Path to the documents with which original doc is comparing
path = 'static/other_documents/doc*.txt'
files = glob.glob(path)
#endregion
rows, found_count, fives_count, rounded_percentage_five, percentage_for_chart_five, fives_for_report, founded_docs_for_report = search_by_five(last_uploaded, 5, original_words, report_fives, files)
context = {
...
}
return render(request, 'result.html', context)
report file
:
['universitetindé™', 'té™hsili', 'alä±ram.', 'mé™n'] was found in static/other_documents\doc1.txt.
...
因为WindowsOS。在 Mac 上试用您的应用程序OS 它将毫无问题地运行)