编辑
在下面的另一个信息中,由于一些有用的输入,这个问题是由使用strftime %s来生成Unix时间戳引起的(这就是被查询的系统所需要的)。Strftime %s与Windows平台不兼容,因此我需要使用另一种方法来生成Unix时间戳。Jez建议了time.time(),这是我已经尝试过的,但是我显然没有在这个过程中的某个地方这么做。我需要将这一部分代码从使用strftime改为time():
if (args.startdate):
from_time=str(int(args.startdate.strftime('%s')) * 1000)
if (args.enddate):
to_time=str(int(args.enddate.strftime('%s')) * 1000)任何帮助或一只牛都非常感谢。:)
/EDIT
我得到了一个Python脚本,它在部署在Apple膝上时似乎运行正常,但当我在Windows机器上运行它时会给出错误消息。我需要一个第三方通过远程执行文件,他只有一个窗口机器,所以我需要尝试找出为什么它不工作。我在跑2.7以供参考。本节似乎是造成错误的地方:
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('-s', "--startdate", help="The start date (included)- format YYYY-MM-DD -- if not specified then earliest available data", type=valid_date)
parser.add_argument('-e', "--enddate", help="The end date (excluded) - format YYYY-MM-DD -- if not specified then 'now'", type=valid_date)
parser.add_argument('-u', "--user", help="User name to use", default='admin')
parser.add_argument('-p', "--password", help="Password for user", default='redwood')
parser.add_argument('-a', "--attribute", help="Attribute", choices=['temperature', 'motion', 'input-power', 'output-power'], default='motion')
parser.add_argument('host', help='Hostname/IP address of engine to connect to', default='127.0.0.1')
args = parser.parse_args()
user = args.user
passwd = args.password
if (args.startdate):
from_time=str(int(args.startdate.strftime('%s')) * 1000)
if (args.enddate):
to_time=str(int(args.enddate.strftime('%s')) * 1000)
scale=1
unit='seconds since 1.1.1970'
if (args.attribute == 'temperature'):
path=temperature_path
scale = 100
unit = "Degrees Celsius"
elif (args.attribute == 'output-power'):
path=opower_path
scale = 100
unit = "Watts"
elif (args.attribute == 'input-power'):
path=ipower_path
scale = 1000
unit = "Watts"
else:
path=motion_path
print "Epoch Time, Local Time (this machine), Attribute, Value (%s) " % unit
query_stats(args.host)这是我用来执行的命令:
C:\Python27>python stats_query.py -s 2016-03-18 -e 2016-03-19 -u admin -p admin -a motion 192.168.2.222这是我得到的错误信息:
Traceback (most recent call last):
File "stats_query.py", line 132, in <module>
from_time=str(int(args.startdate.strftime('%s')) * 1000)
ValueError: Invalid format string如果有人有任何想法,我将非常感谢任何反馈。很抱歉,如果我问了一个愚蠢的问题-我真的不太熟悉Python。
发布于 2016-05-09 13:51:05
如果您想要在几秒钟内打印,请使用%S (带有大写的S)。
发布于 2016-05-09 14:02:56
(也许应该是评论)
正如其他人已经提到的那样,并非所有的指令都可以在所有平台上使用。
Python的文档列出了所有跨平台指令:https://docs.python.org/2/library/datetime.html#strftime-strptime-behavior
发布于 2016-05-09 14:43:12
不确定valid_time在您的函数中是什么,但是如果您使用以下内容:
def valid_time(t):
format = "%Y-%m-%d"
datetime.datetime.strptime(t, format)通过你描述的问题..。在此之后,我看到了其他问题,因为没有定义motion_path。
更新:使用python2.7(文件保存为tryme.py)在Windows7 Professional上运行以下代码
import argparse
import datetime
def motion_path():
print "Got here.. no issues"
def query_stats(host):
print "We'll query the host: %s" % host
def valid_time(t):
format = "%Y-%m-%d"
return datetime.datetime.strptime(t, format)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('-s', "--startdate", help="The start date (included)- format YYYY-MM-DD -- if not specified then earliest available data", type=valid_time)
parser.add_argument('-e', "--enddate", help="The end date (excluded) - format YYYY-MM-DD -- if not specified then 'now'", type=valid_time)
parser.add_argument('-u', "--user", help="User name to use", default='admin')
parser.add_argument('-p', "--password", help="Password for user", default='redwood')
parser.add_argument('-a', "--attribute", help="Attribute", choices=['temperature', 'motion', 'input-power', 'output-power'], default='motion')
parser.add_argument('host', help='Hostname/IP address of engine to connect to', default='127.0.0.1')
args = parser.parse_args()
user = args.user
epoch = datetime.datetime.utcfromtimestamp(0)
passwd = args.password
if (args.startdate):
from_time=str(int((args.startdate-epoch).total_seconds()))
if (args.enddate):
to_time=str(int((args.enddate-epoch).total_seconds()))
print 'From: %s\nTo: %s\n' %(from_time, to_time)
scale=1
unit='seconds since 1.1.1970'
if (args.attribute == 'temperature'):
path=temperature_path
scale = 100
unit = "Degrees Celsius"
elif (args.attribute == 'output-power'):
path=opower_path
scale = 100
unit = "Watts"
elif (args.attribute == 'input-power'):
path=ipower_path
scale = 1000
unit = "Watts"
else:
path=motion_path
print "Epoch Time, Local Time (this machine), Attribute, Value (%s) " % unit
query_stats(args.host)用于运行它的命令:
C:\Python27>python tryme.py -s 2016-03-18 -e 2016-03-19 -u admin -p admin -a motion 192.168.2.222结果:
Epoch Time, Local Time (this machine), Attribute, Value (seconds since 1.1.1970)
We'll query the host: 192.168.2.222https://stackoverflow.com/questions/37117445
复制相似问题