将数据写入 Excel 给我 'ZIP does not support timestamps before 1980'

Writing data to Excel give me 'ZIP does not support timestamps before 1980'

我希望不要创建任何副本,但我环顾四周(stack overflow 和其他论坛),我发现了一些类似的问题,但 none 个问题解决了我的问题。

我有一个 python 代码,唯一要做的就是查询数据库,在 Pandas 中创建一个 DataFrame 并将其写入 Excel 文件。

该代码在本地运行没有问题,但是当我在我的服务器中引入它时,它开始出现此错误:

  File "Test.py", line 34, in <module>
    test()
  File "Test.py", line 31, in test
    ex.generate_file()
  File "/home/carlo/Test/Utility/ExportExcell.py", line 96, in generate_file
    writer.save()
  File "/usr/local/lib/python2.7/dist-packages/pandas/io/excel.py", line 1952, in save
    return self.book.close()
  File "/usr/local/lib/python2.7/dist-packages/xlsxwriter/workbook.py", line 306, in close
    self._store_workbook()
  File "/usr/local/lib/python2.7/dist-packages/xlsxwriter/workbook.py", line 677, in _store_workbook
    xlsx_file.write(os_filename, xml_filename)
  File "/usr/lib/python2.7/zipfile.py", line 1135, in write
    zinfo = ZipInfo(arcname, date_time)
  File "/usr/lib/python2.7/zipfile.py", line 305, in __init__
    raise ValueError('ZIP does not support timestamps before 1980')
ValueError: ZIP does not support timestamps before 1980

为了确保一切正常,我打印了我的 DataFrame,对我来说它看起来不错,因为当我 运行 它在本地生成一个 excell 文件时没有问题:

   Computer_System_Memory_Size  Count_of_HostName   Disk_Total_Size  Number_of_CPU       OS_Family
0                5736053088256                 70     6072238035456         282660         Windows
1                  96159653888                607       96630589440        2451066         vCenter
2                            0                  9                 0          36342  Virtualization
3             2469361287143424                 37  2389533519619072         149406            Unix
4                3691651514368                 90     5817485303808         363420           Linux

我在这里没有看到任何时间戳,这是我的代码的一部分:

pivot = pd.DataFrame.from_dict(pivot) #pivot= information extracted from DB

pd.to_numeric(pivot['Count_of_HostName'], downcast='signed')#try to enforce to be a numeric value in case it get confused with a datetime
pd.to_numeric(pivot['Disk_Total_Size'], downcast='signed')#try to enforce to be a numeric value in case it get confused with a datetime
pd.to_numeric(pivot['Computer_System_Memory_Size'], downcast='signed')#try to enforce to be a numeric value in case it get confused with a datetime
pd.to_numeric(pivot['Number_of_CPU'], downcast='signed')#try to enforce to be a numeric value in case it get confused with a datetime

print pivot
name = 'TempReport/Report.xlsx'#set-up file name
writer = pd.ExcelWriter(name, engine='xlsxwriter')#create excel with file name
pivot.to_excel(writer, 'Pivot', index=False)#introduce my data to excel
writer.save()#write to file, it's where it fail

有人知道为什么它不能在 Ubuntu 16.04 服务器上工作而不给我 'ZIP does not support timestamps before 1980' 错误吗? 我检查了很多东西,库版本,确保没有数据

XlsxWriter 设置组成 XLSX 文件的单个 XML 文件,其创建日期为 1/1/1980,这是(我认为)ZIP 纪元和 Excel 使用的日期.一旦使用相同的输入数据和元数据,这允许 XlsxWriter 创建的文件的二进制再现。

它设置日期如下(对于非内存zipfile.py)情况:

timestamp = time.mktime((1980, 1, 1, 0, 0, 0, 0, 0, 0))
os.utime(os_filename, (timestamp, timestamp))

当此操作以某种方式失败并且日期设置在 1980 年 1 月 1 日之前时,就会出现您看到的错误。

我以前只在用户使用容器并且容器与主机系统的时间不同的情况下看到过一次这种情况。

您是否遇到过这样的情况,或者由于某种原因时间戳设置不正确?

更新:在与失败示例相同的环境中尝试运行:

import os
import time

filename = 'file.txt'
file = open(filename, 'w')
file.close()

timestamp = time.mktime((1980, 1, 1, 0, 0, 0, 0, 0, 0))
os.utime(filename, (timestamp, timestamp))

print(time.ctime(os.path.getmtime(filename)))
# Should give:
# Tue Jan  1 00:00:00 1980

更新:此问题已在 XlsxWriter >= 1.1.9 中修复。

尝试使用此引擎:

pd.to_excel('file_name.xlsx', engine = 'openpyxl')

此问题已在 XlsxWriter 1.2.1 中修复!