2017-08-16 39 views
0

我使用sftp在遠程計算機上打開文件,但它工作正常,但我得到這個錯誤ValueError:時間數據與遠程計算機文件上的格式「%Y-%m-%d%H:%M:%S」不匹配

ValueError: time data '"2015-06-25 14:50:00"' does not match format 
'%Y-%m-%d %H:%M:%S' 

但我使用的格式是正確的。這是我的代碼到代碼的各個部分。

#!/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) 
. 
. 
. 
. 

有人可以幫助我或指導我解決這個問題。我會很感激。

+2

你周圍有時間字符串雙引號,所以你需要剝去這些出或將它們添加到格式字符串''‘%Y-%間%d%H:%M:%S’ '' – EdChum

+0

@EdChum你可以舉一個小例子如何將「」添加到格式字符串。我嘗試了2個解決方案,但他們不工作。 – robbin

+0

@EdChum我試過這個解決方案data_tmp = [v.replace(「\」「,」「)for v in start_date],但導致TypeError:必須是字符串,而不是列表 – robbin

回答

1

它看起來像你的日期字符串用雙引號,您可以包括在格式字符串中的雙引號,或簡單將剝離這些第一:

In[15]: 
d = '"2015-06-25 14:50:00"'.replace('"','') 
datetime.strptime(d, fmt) 

Out[15]: datetime.datetime(2015, 6, 25, 14, 50) 

strip也將在這裏工作:

d.strip('\"') 
在你的代碼

所以添加此

start_date = line[0].replace('"','') 

start_date = line[0].strip('\"') 
+0

非常感謝。 – robbin

相關問題