【问题标题】:Write JSON into Excel and send in response API将 JSON 写入 Excel 并发送响应 API
【发布时间】:2017-12-06 14:08:39
【问题描述】:

我遇到了一个问题,我需要在 API 的响应中发送 excel 文件,但数据量很大。 数据来自 Django 模型查询,然后我对其进行迭代并保存到如下变量:

data=[
  {
    'title': 'xxxx',
    'website': 'xxxx',
    'state': 'CA ',
    'second_followup': None,
    'fourth_followup': datetime.datetime(2017-5-3-0-0-tzinfo=<UTC>),
    'first_followup': None,
    'first_reponse': '',
    'type': 'a',
    'email_status': 'xxx',
    'team_size': 0,
    'second_reponse': '',
    'bed_count': 0,
    'created_at': datetime.datetime(2016-8-30-0-0-tzinfo=<UTC>),
    'aum': 0,
    'bounce': None,
    'lead_status': 'xxxx',
    'founded_year': '0000',
    'third_followup': None,
    'last_name': 'xxxx',
    'address': 'xxxx',
    'email': 'xxxx',
    'created_by': 'xxxx',
    'specialties': 'xx',
    'response_date': None,
    'first_name': 'xxxx ',
    'zip': 'xxxx',
    'company_source': 'xxxx',
    'revenue': 5.0,
    'company_name': 'xxxx',
    'fax_number': '0',
    'inital_contact': datetime.datetime(2017-5-3-0-0-tzinfo=<UTC>),
    'contact_number': '0',
    'domain1': 'xxx',
    'city': '0',
    'country': 'USA',
    'industry': 'xxx',
    'department': 'a',
    'domain2': 'xxx',
    'contact_source': 'xxx',
    'fifth_followup': None
  },
  {
    'title': 'xxxx',
    'website': 'xxxx',
    'state': 'CA ',
    'second_followup': None,
    'fourth_followup': datetime.datetime(2017-5-3-0-0-tzinfo=<UTC>),
    'first_followup': None,
    'first_reponse': '',
    'type': 'a',
    'email_status': 'xxx',
    'team_size': 0,
    'second_reponse': '',
    'bed_count': 0,
    'created_at': datetime.datetime(2016-8-30-0-0-tzinfo=<UTC>),
    'aum': 0,
    'bounce': None,
    'lead_status': 'xxxx',
    'founded_year': '0000',
    'third_followup': None,
    'last_name': 'xxxx',
    'address': 'xxxx',
    'email': 'xxxx',
    'created_by': 'xxxx',
    'specialties': 'xx',
    'response_date': None,
    'first_name': 'xxxx ',
    'zip': 'xxxx',
    'company_source': 'xxxx',
    'revenue': 5.0,
    'company_name': 'xxxx',
    'fax_number': '0',
    'inital_contact': datetime.datetime(2017-5-3-0-0-tzinfo=<UTC>),
    'contact_number': '0',
    'domain1': 'xxx',
    'city': '0',
    'country': 'USA',
    'industry': 'xxx',
    'department': 'a',
    'domain2': 'xxx',
    'contact_source': 'xxx',
    'fifth_followup': None
  },... and so on
]

在上面我有超过 10000 个数据,那么我如何将这些数据写入 excel 文件并将该文件发送到 API 响应,以便用户可以下载文件。文件不会保存在服务器上这是另一个要求

在迭代 Django 模型查询的响应时,JSON 不会保存在它创建的任何文件中

【问题讨论】:

    标签: json django excel python-3.x api


    【解决方案1】:

    将变量发送到客户端,创建一个临时 JSON 文件并使用带有Json2Flat 的javascript 将其转换为.csv

    此库将 JSON 文档转换为 CSV。 它使用 google-Gson 和 JsonPath 进行转换。您可以稍后删除 JSON 文件或将其作为替代发送给客户端。

    【讨论】:

    • 目前,我们正在做同样的事情,将这个变量发送到客户端并使用分页,但是将这些数据呈现到文件中需要很长时间,并且需要很长时间后才会下载。所以我想我需要从后端处理
    • 您无法帮助下载,因为它取决于网络基础设施。看,您有两个选择 - 要么在服务器上创建文件并使其可供下载,要么在一段时间后删除文件,或者以序列化方式发送数据并使用客户端的计算资源创建 CSV。你能想到别的办法吗?
    • 谢谢,我会尝试第一个选项在服务器上创建一个文件,因为目前,我们正在做同样的事情来从客户端处理的服务器发送数据。在服务器端或客户端还有一件事是可能的,如果我一次发送所有数据而不进行分页,那么它将不会出现超时错误。
    • 但如果您想最终生成 CSV,您仍需要将其存储在变量或文件中。让我的答案知道它是否有效:)
    • @ShubhamSrivastava 很高兴 :) 这是一件有趣的事情,我也想尽可能优化效率!
    猜你喜欢
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多