2017-08-11 142 views
0

我想运行下面的python “python get_timestamp.py -f gsham_input.xvg -1 -0.1348 -2 -0.1109”。但是,似乎python错误地使用短划线标记小数点前的负号,并显示以下错误:“File”get_timestamp.py“,第21行,在 value1 = float(arg) ValueError:浮点数无效文字():-0.1348 “ 请你帮我知道如何解决它?我的Python是令人困惑的减号与破折号

谢谢。

#!/usr/bin/env python 

""" 
Given two values, looks in a 3 column-file (output of sham.pl) 
which time frame matches closest. 
""" 

import sys 

USAGE = "USAGE: get_timestamp.py -f <sham output> -1 <value 1> -2 <value 2>\n" 

# Parse arguments 
read_input, read_value1, read_value2 = False, False, False 
input_file, value1, value2 = None, None, None 
for arg in sys.argv[1:]: 
    if read_input: 
     read_input = False 
     input_file = arg 
    elif read_value1: 
     read_value1 = False 
     value1 = float(arg) 
    elif read_value2: 
     read_value2 = False 
     value2 = float(arg) 
    if arg[0] == "-": 
     if arg == "-f": 
      read_input = True 
      continue 
     elif arg == "-1": 
      read_value1 = True 
      continue 
     elif arg == "-2": 
      read_value2 = True 
     else: 
      print USAGE 
      sys.stderr.write('ERROR: Option not recognized: %s\n' %arg) 
      sys.exit(1) 

if not input_file: 
    print USAGE 
    sys.stderr.write('ERROR: You forgot to provide an input file.\n') 
    sys.exit(1) 

# Open sham output 
x_values, y_values, time_values = [], [], [] 
fhandle = open(input_file) 
for line in fhandle: 
    if line[0] != "#" and len(line.split()) == 3: 
     t,x,y = line.split() 
     x_values.append(float(x)) 
     y_values.append(float(y)) 
     time_values.append(float(t)) 
fhandle.close() 

def find_common_frame(min_x, min_y): 
    for xval in min_x: 
     xframe = xval[0] 
     for yval in min_y: 
      yframe = yval[0] 
      if xframe == yframe: 
       return (xframe, xval[1], yval[1]) 
    return (None, None, None) 

# If you cannot find anything, try increasing the nval variable 
nval = 50 
min_x = sorted(enumerate(x_values), key=lambda x: abs(x[1]-value1))[:nval] 
min_y = sorted(enumerate(y_values), key=lambda x: abs(x[1]-value2))[:nval] 

frame, x, y = find_common_frame(min_x, min_y) 

if not frame: 
    print "No timestamp found.." 
    sys.exit(0) 
print "## T = %s (%s, %s)" %(time_values[frame], x, y) 
+2

从查看帖子的格式看来,您使用的是两条不同的破折号(其中一条是M-dash?) – asongtoruin

+1

这不是解析args的非常pythonic方法,而是使用'argparse'模块 –

+0

老实说,我不是很熟悉python,需要使用python作为我的分析的一部分。你有任何建议来编辑它? – Sajad

回答

2

这不是一个Python的问题,要传递的浮点数与minus signs,Unicode的U + 2212),而不是regular hyphens-minus symbols-,Unicode的U + 002D,开始由Python和大多数语言中所使用的那些作为“减号”符号)。我猜这是因为你从一些文档中复制了数字,因为用键盘输入一个Unicode减号相当困难。

简单的解决方案是当您在命令行中调用程序时,用常规连字符代替这些标志。如果你真的需要你的程序有这些标志的工作,你可以使用这样的函数来分析,而不是调用float的数字:

def float_unicode(arg_str): 
    return float(arg_str.decode("utf8").replace(u"\u2212", "-")) 

但是我不会推荐,因为它只是复杂的程序更多,大多数使用数字的命令行工具不支持这一点,所以用户通常不会期望你的程序这样做。

+0

我不知道如何使用关键字的“减号”和“常规连字符”。你能给我一个线索吗? – Sajad

+0

@Sajad你的意思是“关键字”(键盘的错字)?我建议你在调用你的程序之前,只需在命令行中用'-0.1348'(等等)替换“-0.1348”。或者只是用键盘写数字而不是复制和粘贴它们。 “常规连字符”(您需要的那个)是您在键盘上按减号或连字符键时得到的。 – jdehesa

+0

非常感谢您的评论。我在我的命令(python get_timestamp.py -f gsham_input.xvg -1 -0.1348 -2 -0.1109)中的所有选项和数字前面使用了“常规连字符”(键盘上0左侧的按键)。但是,我遇到了这个错误“错误:选项无法识别:-0.1348”。我错过了什么吗? – Sajad