class CSVDownload(View):
""" Prepares CSV file version to download """
#more code here
f = StringIO.StringIO()
writer = csv.writer(f, dialect='excel')
for v in visit_list:
writer.writerow([v.idfa.idfa, v.name, v.duration, v.firstSeen, v.lastSeen, v.identifier, v.closestProximity])
f.seek(0)
response = HttpResponse(f, content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename=AdvertiserData.csv'
return response
出于某种原因,在excel中打开文件时,该文件只会输出列表中的最后一项,请参阅
这让我相信每一行都会覆盖第一行。虽然情况并非如此。看看我在终端中进行的这些测试。
>>> f.getvalue()
'FFAC6F6C-1B2E-47C2-8110-5E619B239FB1,PPTest1,00:00:24,2014-11-11 23:20:24.730000,2014-11-11 23:20:48.750000,nkfe-cnb7s,NEAR\r\nFFAC6F6C-1B2E-47C2-8110-5E619B239FB1,moo2,00:00:24,2014-11-11 23:20:24.730000,2014-11-11 23:20:48.750000,nkfe-cnb7s,NEAR\r\n'
这将返回多个值,值之间为\r\n
。
另外,我试过
>>> print response
Content-Type: text/csv
Content-Disposition: attachment; filename=boo.csv
FFAC6F6C-1B2E-47C2-8110-5E619B239FB1,PPTest1,00:00:24,2014-11-11 23:20:24.730000,2014-11-11 23:20:48.750000,nkfe-cnb7s,NEAR
FFAC6F6C-1B2E-47C2-8110-5E619B239FB1,moo2,00:00:24,2014-11-11 23:20:24.730000,2014-11-11 23:20:48.750000,nkfe-cnb7s,NEAR
这也显示了不止一个值。
我最近在这里看到了一个讨论在这里添加文件的SO线程:How do you append to a file? 但由于我只在内存中创建此项(使用stringIO),我怎样才能获得相同的效果?
谢谢!
编辑有关其他信息:
visit_list
因访问模式而受到影响:
class Visit(models.Model):
idfa = models.ForeignKey(Report)
name = models.CharField(max_length=255)
lastSeen = models.CharField(max_length=255)
duration = models.CharField(max_length=255)
firstSeen = models.CharField(max_length=255)
identifier = models.CharField(max_length=255)
closestProximity = models.CharField(max_length=255)
def __unicode__(self):
return self.name
class Meta:
verbose_name = "Visit"
verbose_name_plural = "Visits"
在这种特殊情况下,visit_list返回:
>>> visit_list
[<Visit: PPTest1>, <Visit: moo2>]
答案 0 :(得分:1)
底线:我怀疑您需要将f.getvalue()
传递给HttpResponse
而不是f
。
解释:
HttpResponse
prefers strings,虽然它也可以被视为文件对象或接受迭代器。然而,f
是一个StringIO.StringIO
实例,而不是字符串本身。
考虑以下设置:
import StringIO
import csv
>>> f = StringIO.StringIO()
>>> writer = csv.writer(f, dialect='excel')
>>> row = range(4) # first row is [0, 1, 2, 3]
>>> for i in range(5):
writer.writerow([row]) # write more rows
row = [x + 1 for x in row]
>>> f.seek(0)
现在将f
与f.getvalue()
的结果进行比较:
# an instance
>>> f
<StringIO.StringIO instance at 0x03656990>
# a string
>>> f.getvalue()
'"[0, 1, 2, 3]"\r\n"[1, 2, 3, 4]"\r\n"[2, 3, 4, 5]"\r\n"[3, 4, 5, 6]"\r\n"[4, 5, 6, 7]"\r\n'
现在请注意将实例与字符串传递给response
时HttpResponse
对象的区别:
# The instance
>>> response = HttpResponse(f, content_type='text/csv') # f is an instance
>>> response['Content-Disposition'] = 'attachment; filename=AdvertiserData.csv'
>>> response._container
['']
# The string
>>> response = HttpResponse(f.getvalue(), content_type='text/csv') # f.getvalue() returns a string
>>> response['Content-Disposition'] = 'attachment; filename=AdvertiserData.csv'
>>> response._container
['"[0, 1, 2, 3]"\r\n"[1, 2, 3, 4]"\r\n"[2, 3, 4, 5]"\r\n"[3, 4, 5, 6]"\r\n"[4, 5 , 6, 7]"\r\n']
请注意,当您将实例传递给_container
时,响应的HttpResponse
属性为空,但当您传递f.getvalue()
字符串时 为空到HttpResponse
。
我会尝试这样做,然后:
>>> response = HttpResponse(f.getvalue(), content_type='text/csv')
>>> # etc.