Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
553 views
in Technique[技术] by (71.8m points)

my python is confusing the minus with dash sign

I am trying to run the following python with " python get_timestamp.py -f gsham_input.xvg -1 ?0.1348 -2 ?0.1109". However, it seems that the python is mistaking the minus sign before the decimals with the dash sign and shows this error: "File "get_timestamp.py", line 21, in value1 = float(arg) ValueError: invalid literal for float(): ?0.1348 " Could you please help me know how to resolve it?

Thanks.

#!/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>
"

# 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
' %arg)
            sys.exit(1)

if not input_file:
    print USAGE
    sys.stderr.write('ERROR: You forgot to provide an input file.
')
    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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It's not a Python problem, the float numbers that you are passing begin with minus signs (?, unicode U+2212) instead of regular hyphens-minus symbols (-, unicode U+002D, the ones used by Python and most languages as "minus" sign). I'd guess it's because you copied the numbers from some document, since it is rather hard to type a Unicode minus sign with a keyboard.

The easy solution is to replace these signs with regular hyphens when you call the program in the command line. If you really need you program to work with these signs, you could use a function like this to parse the numbers instead of calling float:

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

However I wouldn't recommend this, as it just complicates the program more and most command line tools that work with numbers do not support this, so users generally won't expect your program to do so.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...