简体   繁体   English

如何从日期时间字符串中检索年,月,日,小时和分钟?

[英]How do I retrieve the year, month, day, hours and minutes from date-time string?

I am trying to extract from a tensor string "2018/12/31 22:59" the values of year, month, day, hour and minute. 我试图从张量字符串"2018/12/31 22:59"提取年,月,日,小时和分钟的值。 I found this function tf.string_split for this task, but my code is throwing an error 我为此任务找到了此函数tf.string_split ,但是我的代码抛出了错误

Traceback (most recent call last):
  File "path/to/my/file.py", line 12, in <module>
    date = split_date_time[0]
TypeError: 'SparseTensor' object does not support indexing

Here's the code 这是代码

import tensorflow as tf

date_time = tf.placeholder(dtype=tf.string)

day = tf.placeholder(shape=[None], dtype=tf.int32),
month = tf.placeholder(shape=[None], dtype=tf.int32),
year = tf.placeholder(shape=[None], dtype=tf.int32),
hour = tf.placeholder(shape=[None], dtype=tf.int32),
minute = tf.placeholder(shape=[None], dtype=tf.int32)

split_date_time = tf.string_split(date_time, ' ')
date = split_date_time[0]
time = split_date_time[1]

date_splitted = tf.string_split(date, '-')
year = date_splitted[0]
month = date_splitted[1]
day = date_splitted[2]

time_spplitted = tf.string_split(time, ':')
hour = time_spplitted[0]
minute = time_spplitted[1]

with tf.Session() as sess:
    print (sess.run(year, feed_dict={date_time: "2018-12-31 22:59"}))
    print (sess.run(month, feed_dict={date_time: "2018-12-31 22:59"}))
    print (sess.run(day, feed_dict={date_time: "2018-12-31 22:59"}))
    print (sess.run(hour, feed_dict={date_time: "2018-12-31 22:59"}))
    print (sess.run(minute, feed_dict={date_time: "2018-12-31 22:59"}))

You have several problems in your code (mainly because you apparently didn't read any documentation regarding the functions you're trying to use). 您的代码中有几个问题(主要是因为您显然没有阅读任何有关要使用的功能的文档)。 I will just mention a few crucial problems related to the specific problem you're trying to solve (but I strongly encourage you to study the basics of TensorFlow and its computational model). 我将仅提及与您要解决的特定问题相关的一些关键问题(但我强烈建议您学习TensorFlow的基础知识及其计算模型)。

First, as the documentation of tf.string_split states, the first argument of tf.string_split should be a " 1-D string Tensor, the strings to split ". 首先,作为的文档tf.string_split状态的第一个参数tf.string_split应该是“1-d串张量,字符串分割 ”。 However, "2018-12-31 22:59" is a 0-D string tensor. 但是, "2018-12-31 22:59"是一个0-D字符串张量。

Second, tf.string_split returns a tf.SparseTensor , which cannot be indexed! 其次, tf.string_split返回一个tf.SparseTensor ,该索引无法索引!

Here's a possible solution to your problem: 这是您问题的可能解决方案:

import tensorflow as tf

date_time = tf.placeholder(shape=(1,), dtype=tf.string)

split_date_time = tf.string_split(date_time, ' ')

date = split_date_time.values[0]
time = split_date_time.values[1]

split_date = tf.string_split([date], '-')
split_time = tf.string_split([time], ':')

year = split_date.values[0]
month = split_date.values[1]
day = split_date.values[2]

hours = split_time.values[0]
minutes = split_time.values[1]

with tf.Session() as sess:
    year, month, day, hours, minutes = sess.run([year, month, day, hours, minutes],
                                                feed_dict={date_time: ["2018-12-31 22:59"]})
    print("Year =", year)
    print("Month =", month)
    print("Day =", day)
    print("Hours =", hours)
    print("Minutes =", minutes)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将时间列和月份列结合起来制作日期时间 - How can I combine a time column and and Day of month column to make a date-time 如何将日期从日-月-年重新格式化为年-月-日? - How can I reformat date from day-month-year to year-month-day? 如何从 DateTimeField 中提取年、月、日、小时和分钟? - How to extract year, month, day, hour and minutes from a DateTimeField? 如何从DateField对象中提取日,月,年? - How do I extract day,month,year from a DateField object? DataFrame - 给定开始日期、结束日期、每天发生的小时数,我如何计算给定月/年发生的小时数? - DataFrame - Given a start date, end date , hours incurred daily, how do I go about calculating hours incurred for given month/year? 如何从熊猫数据框中提取日期/年/月? - How do I extract the date/year/month from pandas dataframe? 如何将字符串转换为日期对象并分别获取年、月和日? - How can I convert a string into a date object and get year, month and day separately? 如何在python中将日期转换为日/月/年(20 / Jan / 2012) - How do I convert date to Day/Month/Year (20/Jan/2012) in python 将日期转换为字符串并拆分日、月和年 - converting date to string and splitting day,month and year 如何从一年中的一天开始构建Python时间结构? - How do I construct a Python time struct from the day of year?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM