UTF-8 неправильно сохраняет текстовый файл - Python - PullRequest
0 голосов
/ 25 апреля 2020

Мое django приложение читает файл и сохраняет отчет в другом текстовом файле. Все отлично работает, кроме моих языковых букв. Я имею в виду, 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.
...

1 Ответ

0 голосов
/ 25 апреля 2020

Это из-за Windows ОС. Попробуйте ваше приложение на MacOS, оно будет работать без проблем)

...