Python错误TypeError:无法连接“ str”和“ float”对象 [英] Python Error TypeError: cannot concatenate 'str' and 'float' objects
本文介绍了Python错误TypeError:无法连接“ str”和“ float”对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是Python编程的新手。我不断在 str上看到以下错误。
当我添加+ str时,它没有用。
I am new with Python programming. I keep getting the below error on the 'str'. When I added the + str, it didnt work.
wkt = "POINT("+ geoPoint["lat"] +" " + geoPoint["lon"] + ")"
TypeError: cannot concatenate 'str' and 'float' objects
关于如何解决此错误的任何建议?
Any advice on how I can fix this error?
推荐答案
最简单的解决方案如下所示:
The simplest solution would look like this:
wkt = "POINT("+ str(geoPoint["lat"]) +" " + str(geoPoint["lon"]) + ")"
以下内容将更符合公认的Python风格标准:
The following would be more in line with accepted Python stylistic standards:
wkt = "POINT(%f %f)" % (geoPoint["lat"], geoPoint["lon"])
这使用字符串格式
您还可以做一些更好的事情:
You could do something nicer still:
wkt = "POINT({lat} {lon}".format(**geoPoint)
请参见链接页面关于此的更多想法。
See the linked page for more ideas on this.
这篇关于Python错误TypeError:无法连接“ str”和“ float”对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文