如何在列中拆分日期,月份,日期,小时,分钟,秒

时间:2017-05-18 15:07:40

标签: python-3.x pandas datetime

enter image description here

我有一个带有长日期时间列表的.log文件,并希望将它们拆分为列:年,月,日,小时,分钟,秒,毫秒。以下是我需要拆分的列表数据的一部分:

2015-07-15 20:51:40
2015-07-15 20:51:30
2015-07-15 20:51:20
2015-07-15 20:51:10
2015-07-15 20:51:00
2015-07-15 20:50:50
2015-07-15 20:50:49

但是当我编写以下代码时,它会显示毫秒并不会发生:

  

“AttributeError:'list'对象没有属性'read'”

只有一行有效:

t2  int  2014
t3  int  7
t4  int  14
t5  int  21
t6  int  0
t7  int  10

预期产出:

Day Month Year Hour Minute Second Millisecond
15 07 2015 20 51 40
15 07 2015 20 51 30
15 07 2015 20 51 20
15 07 2015 20 51 10
15 07 2015 20 51 00
15 07 2015 20 50 50
15 07 2015 20 50 40

以下是我使用的代码:

from datetime import datetime
from tkinter.filedialog import askopenfilename
dirname=askopenfilename( initialdir='/.log', title= 'please select a directory')
j=pd.read_csv(dirname, header=None, sep=r";")
t1=j[j.columns[0]]
for date_column in t1:
    date_text = datetime.strptime(date_column, '%Y-%m-%d  %H:%M:%S')
    d = date_text
    t2 = d.year
    t3 = d.month
    t4 = d.day
    t5 = d.hour
    t6 = d.minute
    t7 = d.second
    t8 = d.millisecond

2 个答案:

答案 0 :(得分:0)

parser.parse()期待str但你给它list。你需要这样的东西:

for date_column in t1:
    date_text = parser.parse(date_column)
    # do something with date_text

答案 1 :(得分:0)

然后,我使用pandas将它们拆分为DataFrame。

var data = [{"title":"title one","category":["dogs","cats","pets"]},{"title":"title two","category":["gold fish","pets"]}]

var result = [...new Set([].concat(...data.map(e => e.category)))]
console.log(result)