我正在django中创建一个CSV文件,当您访问一个url时它会下载CSV文件,它的工作原理很棒,唯一的好处是我希望在写入文件时将其存储在内存中,而不是在硬盘上。您如何使用导入的CSV
这是针对使用列表创建csv的django
def data_feed_file(request):
open_publications = self.get_publications(user_context)
with open('facebook_feed.csv', 'wb') as csvfile:
filewriter = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
filewriter.writerow(['id', 'title','description'])
for op_pub in open_publications:
filewriter.writerow([op_pub.id, op_pub.name, str(op_pub.description)])
with open('facebook_feed.csv', 'rb') as myfile:
response = HttpResponse(myfile, content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename=facebook_feed.csv'
return response
答案 0 :(得分:2)
您可以将csv文件保存在pandas DataFrame中。像这样:
import pandas as pd
df = pd.read_csv('url_to_csv')
然后pandas库读取csv,您不必将其保存到驱动器中。