我希望不要创建任何重复项,但我环顾四周(堆栈溢出和其他论坛),我发现了类似的问题,但没有一个解决了我的问题。
我有一个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
有人知道为什么在没有给我“1980年之前的ZIP不支持时间戳”错误的情况下,它在Ubuntu 16.04服务器中不起作用的原因吗?
我检查了很多东西,库版本,确保没有数据
最佳答案
XlsxWriter设置了组成XLSX文件的单个XML文件,其创建日期为1980年1月1日(我认为是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中修复。
关于pandas - 将数据写入Excel给我 'ZIP does not support timestamps before 1980',我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54578818/