python - 无法在我的 python 代码中发现错误

标签 python

我正在学习一些 python 编码,并且正在关注 this youtube video也要做一些练习。 现在这是我正在运行的。

import urllib
import operator
from xml.etree.ElementTree import parse
import webbrowser
from showmap import showmap

def getbuses():
    u = urllib.urlopen('http://ctabustracker.com/bustime/map/getBusesForRoute.jsp?route=22')
    data = u.read()
    f = open('rt22.xml', 'wb')
    f.write(data)
    f.close()
    doc = parse('rt22.xml')
    running_buses = {}
    for bus in doc.findall('bus'):
        idbus = int(bus.findtext('id'))
        lat = float(bus.findtext('lat'))
        lon = float(bus.findtext('lon'))
        direction = str(bus.findtext('d'))
        running_buses[idbus] = {lat, lon, direction}
    return running_buses

def print_routes(running_buses):    
    print 'Running buses on route 22:\n'
    for b, (lat, lon, direction) in running_buses.items():
        print '  Bus number: {} \n     - Latitude: {} \n     - Longitude: {}\n     - Direction: {}'.format(b, lat, lon, direction)
    return

def breakline():
    print '____________________________________\n'
    return

def closest_bus():
    running_buses = getbuses()
    officelat, officelon = 41.980262, -87.668452
    diffs = []
    found = False
    print 'Office coordinates: lat {0} lon {1}'.format(officelat, officelon)
    for b, (lat, lon, direction) in running_buses.items():
        if lat > officelat:
            if direction.startswith('North'):
                diff = (officelat - lat) + (officelon - lon)
                diffs.append(tuple((abs(diff), b)))
                found = True
    if found == False:
        print 'No bus going north right now'
    else:
        closbus = min(diffs, key = operator.itemgetter(0))
    return closbus

def main():
    breakline()
    running_buses = getbuses()
    print_routes(running_buses)
    breakline()
    print 'Closest bus going north: {}'.format(closest_bus()[1])
    return

if __name__ == '__main__':
    main()

奇怪的是,对于几辆公交车,程序莫名其妙地混合了经度、纬度和方向。

例如,这是我现在得到的:

root@kali:~/python_training/newtraining# python bussearch.py 
____________________________________

Running buses on route 22:

  Bus number: 1920 
     - Latitude: North West Bound 
     - Longitude: 41.9186187744
     - Direction: -87.6363869407
  Bus number: 1856 
     - Latitude: 41.8836083476 
     - Longitude: South Bound
     - Direction: -87.6310359701
  Bus number: 1764 
     - Latitude: South Bound 
     - Longitude: -87.6317800846
     - Direction: 41.9113892609
  Bus number: 1893 
     - Latitude: South Bound 
     - Longitude: 41.8746980631
     - Direction: -87.6310698311
  Bus number: 1882 
     - Latitude: 41.9857769012 
     - Longitude: -87.669216156
     - Direction: North Bound
  Bus number: 1911 
     - Latitude: 41.978255328 
     - Longitude: -87.6683738559
     - Direction: North Bound
  Bus number: 1400 
     - Latitude: -87.6738596316 
     - Longitude: 42.0089025851
     - Direction: North Bound
  Bus number: 1892 
     - Latitude: North West Bound 
     - Longitude: -87.6453846035
     - Direction: 41.933323362
  Bus number: 1914 
     - Latitude: -87.6671066284 
     - Longitude: South Bound
     - Direction: 41.9671321698
  Bus number: 1723 
     - Latitude: -87.6315689087 
     - Longitude: South Bound
     - Direction: 41.9017485594
  Bus number: 1885 
     - Latitude: South Bound 
     - Longitude: 41.9857135407
     - Direction: -87.6692754667    
____________________________________

Office coordinates: lat 41.980262 lon -87.668452
Traceback (most recent call last):
  File "bussearch.py", line 69, in <module>
    main()
  File "bussearch.py", line 65, in main
    print 'Closest bus going north: {}'.format(closest_bus()[1])
  File "bussearch.py", line 50, in closest_bus
    if direction.startswith('North'):
AttributeError: 'float' object has no attribute 'startswith'

我知道代码不是那么干净,但我从 3 天开始就开始使用 Python 编码,并且我正在学习。我对编码并不陌生,这对我来说没有任何意义...... 我究竟做错了什么?在引入方向之前,一切似乎都运行良好。 有帮助吗?

最佳答案

running_buses[idbus] = {lat, lon, direction} 这一行中,您定义了一个集合,它是一个无序集合。因此,当您在 running_buses.items() 中说 for b, (lat, lon, direction) 时,在某些情况下,您是将纬度或经度分配给 direction 变量.

将第一行更改为 running_buses[idbus] = (lat, lon, direction) 以使用(有序的)元组。

关于python - 无法在我的 python 代码中发现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44680650/

相关文章:

javascript - 如何让spawnSync和fs.readFile先后执行?

python - 使用 iloc 和 bool 掩码设置数据帧(数据帧中多个不同索引(行)值的掩码)

python - Matplotlib 在使用 "Times New Roman"时设置粗体标题

python - 为什么 python/numpy 的 += 会改变原始数组?

Python Pandas数据帧格式索引问题

python - 在 Pandas 中按时间顺序合并日期数据框

python - 绑定(bind)参数 0 时出错 - 可能是不受支持的类型。 SQL/Python

python - Python 2 与 3 中的评估范围

python - 连接上的过滤器不会传播到相关的 orm.relations

python - 无法在 Tensorflow 中加载检查点文件。 (操作系统错误 : file signature not found)