[英]ValueError: time data does not match format '%Y-%m-%d %H:%M:%S' on remote machine file
I open a file on remote machine by using sftp and it work fine, but i got this error 我使用sftp在远程计算机上打开了一个文件,它工作正常,但出现此错误
ValueError: time data '"2015-06-25 14:50:00"' does not match format
'%Y-%m-%d %H:%M:%S'
but the format i am using is correct. 但是我使用的格式是正确的。 Here is my code to respective portion of code.
这是我代码的各个部分。
#!/usr/bin/env python
# -*-coding:utf-8 -*
import os
import sys
import time
import stat
import pysftp as sftp
import subprocess
import paramiko
import datetime
from datetime import datetime, timedelta
from time import mktime, strftime, localtime, sleep
u_name = 'robi'
pswd = 'xxxx'
port = 22
r_ip = 'xxx.xxx.x.xxx'
sec_key = '/home/rob/key_detail'
myconn = paramiko.SSHClient()
myconn.set_missing_host_key_policy(paramiko.AutoAddPolicy())
my_rsa_key = paramiko.RSAKey.from_private_key_file(sec_key)
session = myconn.connect(r_ip, username=u_name, password=pswd, port=port,
pkey=my_rsa_key)
# print myconn.get_transport().is_active()
# path1 = "/home/rob_remote/sensors/12/tem_data/temp.dat"
fmt = '%Y-%m-%d %H:%M:%S'
path1 = "/home/rob_remote/sensors/12/tem_data/temp.dat"
path2 = '/home/new/loc/13/press/pressure.dat'
start_time = datetime.strptime("2015-06-25 14:50:00", fmt)
latest_time = datetime.now()
step_size = 10
diff = latest_time - start_time
minutes_values = diff.total_seconds() / 60
expected = int(minutes_values / step_size)
sftp = myconn.open_sftp()
with sftp.open(path1) as f:
read = f.readlines()[4:]
get_values = []
for line in read:
line = line.strip().split(',')
start_date = line[0]
start_date = datetime.strptime(start_date, fmt)
current_time = datetime.now()
step_size = 5
differ_time = current_time - start_date
minutes_values = differ_time.total_seconds() / 60
get_values.append(int(minutes_values / step_size))
# print 'get_values:::::::::::::::::::', max(get_values)
get_val = max(get_values)
.
.
.
.
Can someone help me or guide me to deal with this problem. 有人可以帮我还是指导我解决这个问题。 I would be thankful.
我会很感激。
It looks like your date strings are enclosed in double-quotes, you can either include the double quotes in the format string, or simpler would be to strip these first: 看来您的日期字符串用双引号引起来,您可以在格式字符串中包括双引号,或者更简单的做法是先将其删除:
In[15]:
d = '"2015-06-25 14:50:00"'.replace('"','')
datetime.strptime(d, fmt)
Out[15]: datetime.datetime(2015, 6, 25, 14, 50)
strip
will also work here: strip
也将在这里工作:
d.strip('\"')
So in your code add this 所以在你的代码中添加
start_date = line[0].replace('"','')
or 要么
start_date = line[0].strip('\"')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.