繁体   English   中英

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.
...

这是因为 Windows 操作系统。 在 MacOS 上尝试您的应用程序,它将毫无问题地运行)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM