【发布时间】:2015-12-14 13:44:19
【问题描述】:
我正在使用 Python 2.7 和 Django 1.7。
我的管理界面中有一个方法可以生成某种 csv 文件。
def generate_csv(args):
...
#some code that generates a dictionary to be written as csv
....
# this creates a directory and returns its filepath
dirname = create_csv_dir('stock')
csvpath = os.path.join(dirname, 'mycsv_file.csv')
fieldnames = [#some field names]
# this function creates the csv file in the directory shown by the csvpath
newcsv(data, csvheader, csvpath, fieldnames)
# this automatically starts a download from that directory
return HttpResponseRedirect('/media/csv/stock/%s' % csvfile)
总而言之,我创建了一个 csv 文件,将其保存在磁盘上的某个位置,然后将其 URL 传递给用户以供下载。
我在想如果所有这些都可以在不写入磁盘的情况下完成。我搜索了一下,也许内容处置附件可能对我有帮助,但我有点迷失在文档中。
无论如何,如果有更简单的方法,我很想知道。
【问题讨论】:
-
查看
StringIO -
看起来这与这里的问题非常相似,因为它几乎相同,只是负载不同:stackoverflow.com/questions/908258/…