将数据帧写入gzip压缩的CSV会将时间戳添加到存档:
import pandas as pd
df = pd.DataFrame({'a': [1]})
df.to_csv('df.csv.gz', compression='gzip')
# Timestamp is the large number per https://unix.stackexchange.com/a/79546/88807.
!<df.csv.gz dd bs=4 skip=1 count=1 | od -t d4
# 1+0 records in
# 1+0 records out
# 4 bytes copied, 5.6233e-05 s, 71.1 kB/s
# 0000000 1546978755
# 0000004df.csv
我希望不使用时间戳来编写它,以便同一数据帧的两个后续导出是相同的:
df.to_csv('df2.csv.gz', compression='gzip')
import filecmp
filecmp.cmp('df.csv.gz', 'df2.csv.gz')
# False
在浏览了Pandas的CSV writing代码之后,我建议最好直接使用} attribute ,这似乎就是您想要的:
gzip
模块。这样您就可以直接设置^{对于这个小数据集,这优于下面的两步
subprocess
方法:我使用
TextIOWrapper()
将字符串转换为字节作为Pandas does处理,但如果您知道不会保存太多数据,也可以这样做:注意,
gzip -lv df.csv.gz
仍然显示“当前时间”,但它只是从inode的mtime中提取这个值。使用hexdump -C
转储显示值保存在文件中,更改文件mtime(使用touch -mt 0711171533 df.csv.gz
)会导致gzip
显示不同的值还要注意,原始的“filename”也是gzip文件的一部分,因此您需要写入相同的名称(或者重写此名称)以使其具有确定性。你知道吗
您可以导出为未压缩的CSV,然后使用
-n
标志调用gzip
,以避免时间戳(这也是不将文件名保存在存档中的说明):相关问题 更多 >
编程相关推荐