我在 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
最佳答案
您可以将 csv 文件保存在 pandas 中数据框。像这样:
import pandas as pd
df = pd.read_csv('url_to_csv')
然后 pandas 库读取 csv,您不必将其保存到驱动器。
关于python - 如何在python中创建一个可以保存在内存中的CSV文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54369016/