Python pandas to_csv zip 格式 [英] Python pandas to_csv zip format

查看:130
本文介绍了Python pandas to_csv zip 格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在通过 to_csv 写入 zip 文件时遇到了一个奇怪的问题.

I am having a peculiar problem when writing zip files through to_csv.

使用 GZIP:

df.to_csv(path_or_buf = 'sample.csv.gz', compression="gzip", index = None, sep = ",", header=True, encoding='utf-8-sig')

提供一个名为sample.csv.gz"的简洁 gzip 文件,在其中我得到了我的 csv 'sample.csv'

gives a neat gzip file with name 'sample.csv.gz' and inside it I get my csv 'sample.csv'

但是,使用 ZIP 时情况会发生变化

df.to_csv(path_or_buf = 'sample.csv.zip', compression="zip", index = None, sep = ",", header=True, encoding='utf-8-sig')

提供名为sample.csv.zip"的 zip 文件,但在其中,csv 也已重命名为sample.csv.zip".从文件中删除额外的.zip"会返回 csv.

gives a zip file with name 'sample.csv.zip', but inside it the csv has been renamed to 'sample.csv.zip' as well. Removing the extra '.zip' from the file gives the csv back.

如何在没有此问题的情况下实现 zip 扩展?我需要有 zip 文件作为我无法绕过的要求.我在 Windows 10 机器上使用 python 2.7.

How can I implement zip extension without this issue? I need to have zip files as a requirement that I can't bypass. I am using python 2.7 on windows 10 machine.

预先感谢您的帮助.

推荐答案

从 1.0.0 版开始使用 dict 作为压缩选项在 Pandas 中非常简单:

It is pretty straightforward in pandas since version 1.0.0 using dict as compression options:

filename = 'sample'
compression_options = dict(method='zip', archive_name=f'{filename}.csv')
df.to_csv(f'{filename}.zip', compression=compression_options, ...)

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_csv.html

这篇关于Python pandas to_csv zip 格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆