简体   繁体   中英

Invalid format for geopy distance

I start in python and I see that my cities with the coordinates are not used, which makes me a format error, because it does not print the coordinates of the city.

from geopy.distance import geodesic


ville1 = input("Entrez la ville de départ : ")
ville2 = input("Entrez la ville d'arrivée : ")


Paris = (48.864716, 2.349014)
Bordeaux = (44.835241, -0.573289)
Marseille = (43.301631, 5.373301)
Lyon = (45.760346, 4.837617)
Toulouse = (43.606225, 1.459017)
Nice = (43.714026, 7.259467)
Nantes = (47.226733, -1.560644)
Montpellier = (43.606620, 3.869740)
Strasbourg = (48.561351, 7.753844)
Rennes = (48.105276, -1.688280)

print(ville1)
print(ville2)
print(geodesic(ville1, ville2).kilometers)

On console

Entrez la ville de départ : Bordeaux
Entrez la ville d'arrivée : Paris
Bordeaux
Traceback (most recent call last):
Paris
  File "D:/Documents/Ynov/Projet_Camion/calcul.py", line 11, in <module>
    print(geodesic(ville1, ville2).kilometers)
  File "C:\Users\me\AppData\Roaming\Python\Python37\site-packages\geopy\distance.py", line 389, in __init__
    super(geodesic, self).__init__(*args, **kwargs)
  File "C:\Users\me\AppData\Roaming\Python\Python37\site-packages\geopy\distance.py", line 164, in __init__
    kilometers += self.measure(a, b)
  File "C:\Users\me\AppData\Roaming\Python\Python37\site-packages\geopy\distance.py", line 410, in measure
    a, b = Point(a), Point(b)
  File "C:\Users\me\AppData\Roaming\Python\Python37\site-packages\geopy\point.py", line 156, in __new__
    return cls.from_string(arg)
  File "C:\Users\me\AppData\Roaming\Python\Python37\site-packages\geopy\point.py", line 391, in from_string
    "Failed to create Point instance from string: unknown format."
ValueError: Failed to create Point instance from string: unknown format.

Your inputs that define ville1 and ville2 are read as strings. Python doesn't know you want these strings to refer to the variables you've defined.

A better way is to put the locations in a dict and access them like so:

from geopy.distance import geodesic

ville1 = input("Entrez la ville de départ : ")
ville2 = input("Entrez la ville d'arrivée : ")

locations = {
    'Paris': (48.864716, 2.349014),
    'Bordeaux': (44.835241, -0.573289),
    'Marseille': (43.301631, 5.373301),
    'Lyon': (45.760346, 4.837617),
    'Toulouse': (43.606225, 1.459017),
    'Nice': (43.714026, 7.259467),
    'Nantes': (47.226733, -1.560644),
    'Montpellier': (43.606620, 3.869740),
    'Strasbourg': (48.561351, 7.753844),
    'Rennes': (48.105276, -1.688280),
}

print(ville1)
print(ville2)
print(geodesic(locations[ville1], locations[ville2]).kilometers)

An example of the above:

Entrez la ville de départ : Paris
Entrez la ville d'arrivée : Nice
Paris
Nice
685.9425720999892

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM