gpt4 book ai didi

python - Django/Python : CSV for-in loop overriding first row each time through

转载 作者:太空宇宙 更新时间:2023-11-03 18:04:21 25 4
gpt4 key购买 nike

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中打开文件时,文件只会输出列表中的最后一项,参见here

这让我相信每一行都会覆盖第一行。尽管情况不应该如此。看看我在终端中执行的这些测试。

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

这也显示出不止一个值(value)。

我最近在这里看到了一个 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>]

最佳答案

底线:我怀疑您需要将 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)

现在将 ff.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'

现在请注意将实例与字符串传递给 HttpResponseresponse 对象的区别:

# 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']

请注意,当您将实例传递给 HttpResponse 时,响应的 _container 属性为空,但当您传递 f 时,响应的 not 属性为空.getvalue() 字符串到 HttpResponse

我会尝试这个,然后:

>>> response = HttpResponse(f.getvalue(), content_type='text/csv')
>>> # etc.

关于python - Django/Python : CSV for-in loop overriding first row each time through,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27139500/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com