strptime提供ValueError:保留未转换的数据:

时间:2019-02-07 16:24:13

标签: python datetime strptime

有文件保留这样的数据;

  hostname;2019-01-09T23:59:38Z ;2019-01-10T00:04:04Z

从这个文件中,我像这样创建新文件; (在Windows env.python 3.7中)

  hostname;2019-01-09 23:59:38;2019-01-10 00:04:04;0:04:26

但是我需要在安装了python 2.7.5的linux中创建相同的文件。 日期字段存在问题,我没有转换日期字段中的秒。

红帽7.5

Python 2.7.5

错误;

Traceback (most recent call last):
File "test.py", line 18, in <module>
End2 =   datetime.strptime(End, '%Y-%m-%dT%H:%M:%SZ')
File "/usr/lib64/python2.7/_strptime.py", line 328, in _strptime
data_string[found.end():])
ValueError: unconverted data remains:

这就是我开发的;

  #!/bin/python
  import datetime 
  import time


  with open(r"/root/backup/backup.txt") as f:
       with open(r"/root/backup/backup2.txt", 'w') as f1:
           for lines in f:

            fields = lines.split(";")
            Hostname = fields[0]
            Start = fields[1]
            End = fields[2]
            Start1 = datetime.strptime(Start, '%Y-%m-%dT%H:%M:%SZ')
            End2 =   datetime.strptime(End, '%Y-%m-%dT%H:%M:%SZ')
            delta = End2 - Start1

            f1.write(Hostname + ";Start:"+str(Start1)+";Backup Finish:"+ str(End2)+";Backup Completed Time:"+ str(delta))

    f1.close()

  f.close()

我期望的输出是;

主机名; 2019-01-09 23:59:38; 2019-01-10 00:04:04; 0:04:26

0 个答案:

没有答案