python - 如何解决 "iterator should return strings, not bytes"

标签 python django python-3.x iterator

我正在尝试导入 CSV 文件,使用表单从客户端系统上传文件。获得文件后,我将提取其中的一部分并在我的应用程序中填充模型。但是,当我遍历上传文件中的行时,出现“迭代器应该返回字符串,而不是字节”错误。我花了几个小时尝试不同的东西并阅读我能找到的所有内容,但似乎无法解决它(注意,我对 Django 比较新 - 运行 1.5 - 和 python - 运行 3.3)。我删除了一些东西以解决错误并像这样运行它以确保它仍然存在。在tools_clubs_import()中执行“for clubs in club_list”行时出现错误:

根据下面标记的答案,以下是正确的 views.py:

import csv
from io import TextIOWrapper
from django.shortcuts import render
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from rank.forms import ClubImportForm

def tools_clubs_import(request):
    if request.method == 'POST':
        form = ClubImportForm(request.POST, request.FILES)
        if form.is_valid():
            # the following 4 lines dumps request.META to a local file
            # I saw a lot of questions about this so thought I'd post it too
            log = open("/home/joel/meta.txt", "w")
            for k, v in request.META.items():
                print ("%s: %s\n" % (k, request.META[k]), file=log)
            log.close()
            # I found I didn't need errors='replace', your mileage may vary
            f = TextIOWrapper(request.FILES['filename'].file,
                    encoding='ASCII')
            club_list = csv.DictReader(f)
            for club in club_list:
                # do something with each club dictionary entry
                pass
            return HttpResponseRedirect(reverse('rank.views.tools_clubs_import_show'))
    else:
        form = ClubImportForm()

    context = {'form': form, 'active_menu_item': 4,}
    return render(request, 'rank/tools_clubs_import.html', context)

def tools_clubs_import_show(request):
    return render(request, 'rank/tools_clubs_import_show.html')

以下是我提交的原始版本(生成表单的html包含在此代码列表的底部:

views.py
--------
import csv
from django.shortcuts import render
from django.http import HttpResponseRedirect
from rank.forms import ClubImportForm

def tools(request):
    context = {'active_menu_item': 4,}
    return render(request, 'rank/tools.html', context)

def tools_clubs(request):
    context = {'active_menu_item': 4,}
    return render(request, 'rank/tools_clubs.html', context)

def tools_clubs_import(request):
    if request.method == 'POST':
        form = ClubImportForm(request.POST, request.FILES)
        if form.is_valid():
            f = request.FILES['filename']
            club_list = csv.DictReader(f)
            for club in club_list:
                # error occurs before anything here is executed
                # process here... not included for brevity
            return HttpResponseRedirect(reverse('rank.views.tools_clubs_import_show'))
    else:
        form = ClubImportForm()

    context = {'form': form, 'active_menu_item': 4,}
    return render(request, 'rank/tools_clubs_import.html', context)

def tools_clubs_import_show(request):
    return render(request, 'rank/tools_clubs_import_show.html')

forms.py
--------
from django import forms


class ClubImportForm(forms.Form):
    filename = forms.FileField(label='Select a CSV to import:',)


urls.py
-------
from django.conf.urls import patterns, url
from rank import views

urlpatterns = patterns('',
    url(r'^tools/$', views.tools, name='rank-tools'),
    url(r'^tools/clubs/$', views.tools_clubs, name='rank-tools_clubs'),
    url(r'^tools/clubs/import$',
        views.tools_clubs_import,
        name='rank-tools_clubs_import'),
    url(r'^tools/clubs/import/show$',
        views.tools_clubs_import_show,
        name='rank-tools_clubs_import_show'),
)


tools_clubs_import.html
-----------------------
{% extends "rank/base.html" %}
{% block title %}Tools/Club/Import{% endblock %}
{% block center_col %}

    <form enctype="multipart/form-data" method="post" action="{% url 'rank-tools_clubs_import' %}">{% csrf_token %}
        {{ form.as_p }}
        <input type="submit" value="Submit" />
    </form>

{% endblock %}

异常值:

迭代器应该返回字符串,而不是字节(您是否以文本模式打开文件?)

异常位置:/usr/lib/python3.3/csv.py 在字段名中,第 96 行

最佳答案

request.FILES 为您提供 binary 文件,但 csv 模块希望使用文本模式文件。

您需要将文件包装在 io.TextIOWrapper() instance 中,你需要弄清楚编码:

from io import TextIOWrapper

f = TextIOWrapper(request.FILES['filename'].file, encoding=request.encoding)

如果提供了 Content-Type header 中的 charset 参数可能会更好;这就是客户告诉你的字符集。

您无法解决需要知道文件数据的正确编码的问题;您可以强制解释为 ASCII,例如,通过提供 errors 关键字(将其设置为“replace”或“ignore”),但这确实会导致数据丢失:

f = TextIOWrapper(request.FILES['filename'].file, encoding='ascii', errors='replace')

使用 TextIOWrapper 仅在使用 Django 1.11 及更高版本(如 this changeset added the required support )时才有效。在早期版本中,您可以在事后对支持进行猴子补丁:

from django.core.files.utils import FileProxyMixin

if not hasattr(FileProxyMixin, 'readable'):
    # Pre-Django 1.11, add io.IOBase support, see
    # https://github.com/django/django/commit/4f474607de9b470f977a734bdd47590ab202e778        
    def readable(self):
        if self.closed:
            return False
        if hasattr(self.file, 'readable'):
            return self.file.readable()
        return True

    def writable(self):
        if self.closed:
            return False
        if hasattr(self.file, 'writable'):
            return self.file.writable()
        return 'w' in getattr(self.file, 'mode', '')

    def seekable(self):
        if self.closed:
            return False
        if hasattr(self.file, 'seekable'):
            return self.file.seekable()
        return True

    FileProxyMixin.closed = property(
        lambda self: not self.file or self.file.closed)
    FileProxyMixin.readable = readable
    FileProxyMixin.writable = writable
    FileProxyMixin.seekable = seekable

关于python - 如何解决 "iterator should return strings, not bytes",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16243023/

相关文章:

使用 Pandas 数据框进行 Python 计算

python - APScheduler 选项

python - 我如何知道 request.user 来自 Django View 中的位置?

python - 类型错误 : Can't convert 'bytes' object to str implicitly in python

Django 替代 EMAIL_HOST 设置

python - 使用 Pyrex 编译 django 项目

python - 从给定列表中查找包含 int 值的缺失字符串

Python:list() 函数搞乱了 map()

python - 如何根据某些条件为 spark 数据框中的记录分配等级?

Python:为什么将 int 与字符串进行相等比较不会引发错误?