@font-face { font-family: 宋体 }
@font-face { font-family: 宋体 }
@font-face { font-family: Calibri }
@font-face { font-family: “@宋体” }
p.MsoNormal, li.MsoNormal, div.MsoNormal { margin: 0 0 0.0001pt; text-align: justify; text-justify: inter-ideograph; font-size: 10.5pt; font-family: “Calibri”, “sans-serif” }
p.MsoHeader, li.MsoHeader, div.MsoHeader { mso-style-link: “页眉 Char”; margin: 0 0 0.0001pt; text-align: center; layout-grid-mode: char; border: none; padding: 0; font-size: 9pt; font-family: “Calibri”, “sans-serif” }
p.MsoFooter, li.MsoFooter, div.MsoFooter { mso-style-link: “页脚 Char”; margin: 0 0 0.0001pt; layout-grid-mode: char; font-size: 9pt; font-family: “Calibri”, “sans-serif” }
pre { mso-style-link: “HTML 预设格式 Char”; margin: 0 0 0.0001pt; font-size: 12pt; font-family: 宋体 }
p.MsoAcetate, li.MsoAcetate, div.MsoAcetate { mso-style-link: “批注框文本 Char”; margin: 0 0 0.0001pt; text-align: justify; text-justify: inter-ideograph; font-size: 9pt; font-family: “Calibri”, “sans-serif” }
span.Char { mso-style-name: “页眉 Char”; mso-style-link: 页眉 }
span.Char0 { mso-style-name: “页脚 Char”; mso-style-link: 页脚 }
span.Char1 { mso-style-name: “批注框文本 Char”; mso-style-link: 批注框文本 }
span.HTMLChar { mso-style-name: “HTML 预设格式 Char”; mso-style-link: “HTML 预设格式”; font-family: 宋体 }
.MsoChpDefault { font-family: “Calibri”, “sans-serif” }
@page WordSection1 { size: 595.3pt 841.9pt margin-top: 72pt margin-right: 90pt margin-bottom: 72pt margin-left: 90pt layout-grid: 15.6pt }
div.WordSection1 { page: WordSection1 }

最近在学习python将数据导入到excel,发现日期变成数字而不是日期格式的问题。

第一眼看去肯定是excel单元格格式问题,一般excel单元格格式为常规,而常规处理日期时就显示为数字,所以就想到用一个特定的数字(知道具体日期的),增加一天对应的数字就加1

先看看python直接导入后日期的样子(下面时我随机举例的):

 

 

我们需要日期数据替换成标准的日期格式,具体的思路是:

1、先用excel实验2019-5-02对应的日期时间戳是43587

 

2、再用2019-5-0243587看是从什么时候开始计算,结论是1899-12-30

import pandas as pd
pd.to_datetime(
\'2019-05-02\')-pd.Timedelta(\'43587D\') #D表示天 #Timestamp(\'1899-12-30 00:00:00\')

3、那么最后时间戳转化为日期:就只需要当前时间戳+1899-12-30

 

批量转化(定义转化日期戳的函数,dates为日期戳)

方法一:用pd中得Timedeltato_datetime

import pandas as pd

def date(stamp): #这个有个弊端,输入得时间戳过大后报错,最大支持多少106751
    delta = pd.Timedelta(str(stamp)+\'D\')
    real_time = pd.to_datetime(\'1899-12-30\') + delta
    return real_time 

方法二:用datetime模块得timedeltadatetime.strptime

import datetime

def date(dates):
    delta=datetime.timedelta(days=dates)
    today=datetime.datetime.strptime(\'1899-12-30\',\'%Y-%m-%d\')+delta
  #将1899-12-30转化为可以计算的时间格式并加上要转化的日期戳
    return datetime.datetime.strftime(today,\'%Y-%m-%d\')#制定输出日期的格式

 

以下代码是将excel时间戳转化成标准日期,并替换原有列名的具体步骤:

 

import openpyxl
import datetime

data = openpyxl.load_workbook(r\'E:\work_hot\temp\docx\time_test.xlsx\')

def date(dates):
    delta=datetime.timedelta(days=dates)
    today=datetime.datetime.strptime(\'1899-12-30\',\'%Y-%m-%d\')+delta
    return datetime.datetime.strftime(today,\'%Y/%m/%d\') 

list1 = []
for i in data[\'Sheet1\'].columns:
    list1.append([])
for row in data[\'Sheet1\'].values: for x in range(len(row)): realtime = date(row[x]) #转化为具体时间,调用date方法 list1[x].append(realtime) continue #遍历表格中的数据,将原值改为真实日期 i = 0 for row in data[\'Sheet1\'].columns: for j in range(len(list1[i])): row[j].value = list1[i][j] i += 1 data.save(r\'E:\work_hot\temp\docx\time_test1234.xlsx\') #我这里另存了一份,也可以保存到原文件中

结果图:

 

 

 

 

版权声明:本文为pinpin原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/pinpin/p/10795037.html