【问题标题】:ValueError: time data '140120 1520' does not match format '%Y-%m-%d %H:%M:%S'ValueError: 时间数据 '140120 1520' 与格式 '%Y-%m-%d %H:%M:%S' 不匹配
【发布时间】:2017-06-04 03:34:37
【问题描述】:

我收到了这个错误

ValueError: time data '140120 1520' does not match format '%Y-%m-%d %H:%M:%S'

我有一个 csv 文件,我必须将它存储在 db 中。 csv文件中的数据看起来

# date   day  time   rec_no   tem        X        Z             G       

140120    20  1520   920      0.00       0.0    51           0.00         
140120    20  1521   921      37.73      46     -596.05      1.21     
140120    20  1522   922      31.11      42     31.4000      0.50      
140120    20  1523   923      0.00       0.0    -451.50      0.00         
140120    20  1524   924      0.00       0.0    -31.500      0.00      

我的问题是我必须将第一列(日期)和第三列(时间)结合起来,并根据纪元时间将其转换为时间戳,然后再存储到 MySQL 数据库中。 来自传感器的日期格式,我们在 csv 文件中得到的是

140120 
14 represent 2014, 01 represent the month (January) and 20 represent day.

时间到了

1520
15 represent hour and 20 represent minute. and nothing for seconds 
so we can append 00 for seconds.  

其中天列包含与日期最后 2 位相同的信息,所以我忽略了这一列(天)。 所以为了转换成时间戳,我需要结合日期和时间列。我这样做了,然后重建列表,如我的代码所示。

我在网上搜索并找到了转换日期和时间格式的不同技术,但没有符合我需要的命令或解决方案。 我获取上述 csv 文件数据并转换时间戳的代码如下。

with open(item[1]) as f:
        lines = f.readlines()
    for k, line in enumerate(lines):
        if k >= (int(skip_header_line) + int(index_line_number)):
            data_tmp = line.split() 
            print data_tmp  # i got ['140120', '20', '1520', '920', '0.00', '0.0', '51', '0.00', '0', '0.00', '0', '0.00']
            newcolumn = data_tmp[0] + ' ' + data_tmp[2]
            data = [newcolumn] + data_tmp[3:] # re-build the list so we get 
                                              ['140120 1520', '920', '0.00', '0.0', '-31.50', '0.00', '0', '0.00', '0', '0.00']

            strDate = data[0].replace("\"", "")
            print strDate  # i got here  140120 1520
            timestamp = datetime.datetime.strptime(strDate,
                                                   '%Y-%m-%d %H:%M:%S')
            ts = calendar.timegm(timestamp.timetuple())
            data_buffer = []
            for val in data_tmp:
                if val == " ":
                    val = None
                    data_buffer.append(val)
                else:
                    data_buffer.append(float(val))
            cursor.execute(add_data, data_buffer)
            cnx.commit()

cursor.close()
cnx.close()

如果有人给我一个将“140120 1520”转换为时间戳的提示或示例,我将非常感激,因为我不知道如何处理这个问题。谢谢

【问题讨论】:

    标签: python csv timestamp


    【解决方案1】:
    print datetime.datetime.strptime(strDate,'%y%m%d %H%M')
    

    了解更多reference

    编辑: 更新完整代码

    140120 1520 不是浮点值

    删除空格

    【讨论】:

    • 非常感谢您的回复。您的解决方案似乎也有效,但我现在又遇到了另一个错误。 ValueError:float() 的无效文字:140120 1520.........
    【解决方案2】:

    试试'%y%m%d %H%M'。值得指出的是,您需要使用 %y 而不是 %Y 以匹配没有世纪的年份,例如 2014 年的“14”。

    from datetime import datetime
    
    s = '140120 1520'
    print(datetime.strptime(s, '%y%m%d %H%M'))
    

    输出

    2014-01-20 15:20:00
    

    【讨论】:

    • 感谢您的回复。我会在上面提到的代码上尝试这个,很快就会回来。
    • 现在我遇到了另一个问题 ValueError: invalid literal for float(): 140120 1520 你能在我上面提到的代码中进行这些更改吗?我想我犯了一些愚蠢的错误。非常感谢您的帮助
    • @Rio 请更新您原始帖子中的代码以反映您现在正在尝试的内容。
    • 我用新的错误更新了我的问题。我会非常感谢您的帮助。
    • @Rio 您没有更新代码(例如,您仍然显示错误的时间格式字符串)。
    猜你喜欢
    • 2016-10-15
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-11
    相关资源
    最近更新 更多