使用csv.reader读取上传的文件

时间:2017-02-17 08:19:11

标签: django

任务:读取上传的文件以检查结构。我的测试上传文件有5行标题和大约20-30列。编码是ISO-8859-1

听起来很简单,但它让我慢慢陷入精神错乱。 目前唯一可行的解​​决方案是绕道而行:

list
|> Enum.chunk_every(2)
|> Enum.into(%{}, fn [a, b] -> {a, b} end)

肮脏,疯狂,远非可接受

非工作解决方案:
1:

file = request.FILES.getlist('job_file', None)[0]
newdoc = models.Jobs(job_file=file)
newdoc.save()
with codecs.open(newdoc.job_file.name, "r", encoding='iso-8859-1') as fp:
    file_content = list(csv.reader(fp, delimiter=';', quotechar='"'))

2:

file_content = list(csv.reader(file, delimiter=';', quotechar='"'))
print(file_content)
>>>_csv.Error: iterator should return strings, not bytes (did you open the file in text mode?)

3:

file_content = list(csv.reader(file.open('r'), delimiter=';', quotechar='"'))
print(file_content)
>>> TypeError: argument 1 must be an iterator

一些提示:

file_content = list(csv.reader(file.read(), delimiter=';', quotechar='"'))
print(file_content)
>>>_csv.Error: iterator should return strings, not int (did you open the file in text mode?)

请救救我!

2 个答案:

答案 0 :(得分:2)

无需打开文件,您可以将上传的文件转换为TextIOWrapper。这是更清洁的例子

from io import StringIO

file = request.FILES.getlist('job_file', None)[0]
newdoc = models.Jobs.objects.create(job_file=file)
fp = StringIO(file.read(), encoding='iso-8859-1')
file_content = list(csv.reader(fp, delimiter=';', quotechar='"'))

答案 1 :(得分:0)

uploaded = request.FILES.getlist('job_file', None)[0]
decoded_file = uploaded_file.read().decode('ISO-8859-1').splitlines()
file_content = list(csv.reader(decoded_file, delimiter=';', quotechar='"'))