【问题标题】:how to split the csv file in this case [closed]在这种情况下如何拆分csv文件[关闭]
【发布时间】:2022-07-21 16:31:47
【问题描述】:
    _filename = f'summary_download_{_type}_{_from}_to_{_to}_{_encode}{_account}.zip'
    attachment = 'attachment; filename*=utf-8\'\'' + urllib.parse.quote(_filename)

    if _type == 'query':
        header = ['date', 'user_name', 'sdgid', 'number_of_query', 'number_of_device']
    if _type == 'threat':
        header = ['date', 'user_name', 'sdgid', 'threat', 'number_of_query']
    if _type == 'category':
        header = ['date', 'user_name', 'sdgid', 'category', 'number_of_query']
    if _type == 'specified_domain':
        header = ['date', 'user_name', 'sdgid', 'specified_domain_list', 'number_of_query']

    auditlog_data_frame = pd.DataFrame(data, columns=header)
    auditlog_data_csv = auditlog_data_frame.to_csv(index=False, encoding='utf8')
    auditlog_data_csv.split(';')
    with TemporaryDirectory() as tmpdirname:
        temp_zip_file_name = os.path.join(tmpdirname, os.path.basename(_filename))
        zip_file = pyzipper.AESZipFile(
            temp_zip_file_name,
            'w',
            compression=pyzipper.ZIP_DEFLATED,
            encryption=pyzipper.WZ_AES
        )
        zip_file.setpassword(settings.ZIP_FILE_PASSWORD)
        zip_file.writestr('summarydownload.csv', auditlog_data_csv)
        zip_file.close()

        response = HttpResponse(
            open(temp_zip_file_name, 'rb'),
            content_type='application/zip'
        )
    response['Content-Disposition'] = attachment
    return response

【问题讨论】:

  • edit您的帖子包含您对问题的任何其他信息。避免在 cmets 中添加它,因为它们更难阅读并且更容易删除。帖子的编辑按钮就在帖子标签的下方。

标签: python


【解决方案1】:

如果我正确理解了您的问题,那么您正在寻找类似的东西

with open(filename) as input_file:
    output_file = None
    for index, line in enumerate(input_file):
        if index % 1_000_000 == 0:
            if output_file:
                output_file.close()
            output_file = open(f'output{index}', "w")
        output_file.write(line)
    output_file.close()

【讨论】:

  • 从 Python 3.6 开始,您可以在数字文字中使用 下划线(请参阅 PEP515):million = 1_000_000 以提高可读性,甚至还有像 filesplit 这样的库:@ 987654325@
猜你喜欢
  • 1970-01-01
  • 2019-01-15
  • 2020-12-27
  • 2021-11-22
  • 1970-01-01
  • 2018-11-22
  • 1970-01-01
  • 2021-02-13
  • 1970-01-01
相关资源
最近更新 更多