Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在UTF-8-Python中写入txt文件_Python_Django_Python 3.x_Django Views_Python Textprocessing - Fatal编程技术网

在UTF-8-Python中写入txt文件

在UTF-8-Python中写入txt文件,python,django,python-3.x,django-views,python-textprocessing,Python,Django,Python 3.x,Django Views,Python Textprocessing,我的django应用程序从用户那里获取文档,创建了一些关于它的报告,并写入txt文件。有趣的问题是,在我的Mac OS上,一切都运行得很好。但在Windows上,它无法读取某些字母,将其转换为等符号™,ä±。这是我的密码: views.py: def result(request): last_uploaded = OriginalDocument.objects.latest('id') original = open(str(last_uploaded.document),

我的django应用程序从用户那里获取文档,创建了一些关于它的报告,并写入
txt
文件。有趣的问题是,在我的Mac OS上,一切都运行得很好。但在Windows上,它无法读取某些字母,将其转换为
等符号™
ä±
。这是我的密码:

views.py

def result(request):
    last_uploaded = OriginalDocument.objects.latest('id')
    original = open(str(last_uploaded.document), 'r')
    original_words = original.read().lower().split()
    words_count = len(original_words)
    open_original = open(str(last_uploaded.document), "r")
    read_original = open_original.read()
    characters_count = len(read_original)
    report_fives = open("static/report_documents/" + str(last_uploaded.student_name) + 
    "-" + str(last_uploaded.document_title) + "-5.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)
报告txt文件

['universitetindé™', 'té™hsili', 'alä±ram.', 'mé™n'] was found in static/other_documents\doc1.txt.
...

这里的问题是,您对文件调用
open()
,而没有指定编码。如中所述,默认编码取决于平台。这可能就是为什么你会在Windows和MacOS上看到不同的结果

假设文件本身实际上是用UTF-8编码的,只需在读取文件时指定:

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

伙计,在将此属性添加到该行后,结果消失:(甚至由于ms word的未知字符错误导致报告文档文件未打开。我发现了问题!非常感谢!