属性lat必须是在我的仪表板中显示的浮点

2024-10-01 15:48:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我创建了一个googleappengine项目,它接受一个.txt文件,查找文件中的位置,并使用Yahoo Placemaker在地图上绘制一个maker来表示.txt文件。 当我在本地应用程序上运行时,我尝试在本地应用程序上运行时出错:

BadValueError: Property lat must be a float

我的主.py看起来像这样:

class Story(db.Model):
    id = db.StringProperty()
    loc_name = db.StringProperty()
    title = db.StringProperty()
    lat = db.FloatProperty()
    long = db.FloatProperty()
    link = db.StringProperty()

class MyStories(webapp.RequestHandler):
    def get(self):
        temp = db.Query(Story)
        temp = temp.count()


        story_set = Story.all()

        template_values = {
            'storyTemp': story_set
        }

        path = os.path.join(os.path.dirname(__file__), 'index.html')
        self.response.out.write(template.render(path, template_values))

class place(webapp.RequestHandler):
    def get(self):
        path = '/Users/kimmasterson/storing/txtFiles'

           try:
            for infile in glob.glob(os.path.join(path, '*.txt')):
                    #print infile
                    f = open(infile, 'r')
                    data = f.read()
                    newfile = infile.replace('.txt', '')
                    newfile = newfile.replace('/Users/kimmasterson/storing/txtFiles/', '')
                    #print newfile
                    storyname = 'http://www.independent.ie/national-news/' + newfile
                    #print storyname
                    #print newfile
                    #logging.info(data)
                    p = placemaker('HSnG9pPV34EUBcexz.tDYuSrZ8Hnp.LowswI7TxreF8sXrdpVyVIKB4uPGXBYOA9VjjF1Ca42ipd_KhdJsKYjI5cXRo0eJM-')
                    print p.find_places(data)
                    for place in p.places:
                        splitted = place.name.split()
                        for word in splitted:
                            temp = db.Query(Story)
                            temp = temp.filter("link = ", storyname)
                            results = temp.fetch(limit=1)
                            if len(results) >0:
                                break
                            elif 'IE' in word:
                                print temp
                                print 'success'
                                story = Story(name=newfile, lat=place.centroid.latitude, long=place.centroid.longitude, link=storyname, loc_name = place.name, title = newfile).put()
        except:
            print 'error'
            logging.info('BIG FAT ERROR')

def main():
    application = webapp.WSGIApplication([('/', MyStories), ('/place', place)],
                                         debug=True)

    wsgiref.handlers.CGIHandler().run(application)


if __name__ == '__main__':
    main()

我的山药公司名称:

^{pr2}$

出于某种原因,它添加了位置并将文件链接到本地主机上的地图。你知道在这两个地方相同的代码如何在一个地方工作而在另一个地方不能工作吗?在


Tags: 文件pathnameintxtdbplacetemp
2条回答
  1. 最明显的是:path = '/Users/kimmasterson/storing/txtFiles'-我希望你没有把它上传到Appengine,因为显然这在远程服务器上是行不通的。

  2. 如果打印出有关place的一些详细信息,在错误发生之前您会得到什么?

编辑:

看看你想做什么,我认为你实际上不需要上传文本文件。你想从网上的故事中找到位置独立的网站,对吧?您只需将URL传递给placemaker:

p.find_places("http://www.independent.ie/national-news/patient-too-large-for-ambulance-2619414.html")

雅虎将从独立的而不是直接从应用程序发送。在

显而易见的答案是lat(这是从place.centroid.latitude得到的)不是一个浮点数。尝试记录type(place.centroid.latitude)以查看它是什么类型的-我猜它是一个字符串,您需要对它调用float()来解析它。在

正如@Thomas指出的那样,显然不能引用本地机器上的文件,并期望它在上传到appengine后能够正常工作。相反,将静态数据放在应用程序的根目录(或子目录)中,并用相对路径打开它。最简单、最可靠的方法如下:

path = os.path.join(os.path.dirname(__file__), 'path/to/your/file')

相关问题 更多 >

    热门问题