Python TypeError: 不支持的操作数类型 -: 'unicode' 和 'unicode', coords

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14308639/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:02:50  来源:igfitidea点击:

TypeError: unsupported operand type(s) for -: 'unicode' and 'unicode', coords

pythonhtmldatabaseunicode

提问by user1869558

full code is HERE

完整代码在这里

HTML code

HTML代码

<input type="hidden" id="Latitude" name="Latitude" value={{Longitude}} />
<input type="hidden" id="Longitude" name="Longitude" value={{Longitude}} />

document.getElementById("Latitude").value  =  position.coords.latitude;
document.getElementById("Longitude").value =  position.coords.longitude;    

app.py

应用程序

Latitude = request.form['Latitude']
Longitude = request.form['Longitude']

messages = database.returnMessagesinRange(float(Latitude),float(Longitude))

database.py

数据库.py

def returnMessagesinRange(longitude,latitude):
    allMessages = Messages.find()
    messagesinRange = []
    for current in allMessages:
        if ((current['longitude']-longitude) * (current['longitude']-longitude) + (current['latitude']-latitude)*(current['latitude']-latitude)) <= 1:
            if messagesinRange == None:
                messagesinRange = [current['text']]
            else:
                messagesinRange.append(current['text'])
    return messagesinRange

When this is run, i get

当它运行时,我得到

if ((current['longitude']-longitude) * (current['longitude']-longitude) + (current['latitude']-latitude)*(current['latitude']-latitude)) <= 1:
TypeError: unsupported operand type(s) for -: 'unicode' and 'unicode'

Anyone know why this is happening? thanks.

有谁知道为什么会这样?谢谢。

采纳答案by asermax

Both the longitude and latitude retrieved from the request and the database are strings (unicode strings) and you are trying to operate on them as if they were numbers.

从请求和数据库中检索到的经度和纬度都是字符串(unicode 字符串),您正试图对它们进行操作,就好像它们是数字一样。

You should first get the intor floatrepresentation of such strings to be able to operate on them as numbers (using -, *, etc)

你应该先获得intfloat这样的字符串的表示要能够对他们的数字操作(使用-*等等)

You can do that by creating a intor floatobject passing the string as a parameter

您可以通过创建一个intfloat对象将字符串作为参数传递来做到这一点

latitude = int(request.form['Latitude'])

or

或者

latitude = float(request.form['Latitude'])

回答by Eric

current['longitude']and longitudeare both unicodestrings. You need to convert them to floats if you plan to subtract them.

current['longitude']并且longitude都是unicode字符串。float如果您打算减去它们,则需要将它们转换为s。

回答by Paulo Scardine

Unlike in PHP, Python will not auto-convert from string to float. Use:

与 PHP 不同,Python 不会自动从字符串转换为浮点数。用:

errors = []
try:
    latitude = float(request.form['Latitude'])
except ValueError:
    # do something about invalid input
    latitude = 0.0
    errors.append(u"Invalid input for Latitude.")

回答by user1869558

ok we did this which works..

好的,我们这样做了,有效..

def returnMessagesinRange(longitude,latitude):
allMessages = Messages.find()
longitude = (eval(str(longitude)))
latitude = (eval(str(latitude)))
messagesinRange = []
for current in allMessages:
    y=eval(str(current['longitude']))
    x=eval(str(current['latitude']))
    if ((longitude-x)*(longitude-x))+((latitude-y)*(latitude-y)) <= 1:
        if messagesinRange == None:
            messagesinRange = [str(current['text'].encode('ascii','ignore'))]
        else:
            messagesinRange.append(str(current['text'].encode('ascii','ignore')))
return messagesinRange

finally I can see the ending!

终于可以看到结局了!