2016-08-10 23 views
0

Ich benutze das Python-Google-Orte-Skript, aber ich habe ein Problem bei der Integration des Codes in eine Funktion mit zwei Parametern aufgerufen: "MyLocation" und "MyType".Python - Verketten Python Abfrage

"MyLocation" funktioniert gut, aber "MyType" funktioniert nicht.

Code, der (ohne Variable für "MyType") arbeitet

# Configuration de l'encodage (a mettre dans tous les scripts) 
# encoding: utf-8 

#################################### 
### CLASS 
#################################### 
def ExtractGoogleHotspots(Location,MyType): 
     from googleplaces import GooglePlaces, types, lang 
     YOUR_API_KEY = 'XXXXXXX' 
     google_places = GooglePlaces(YOUR_API_KEY) 
     # You may prefer to use the text_search API, instead. 
     query_result = google_places.nearby_search(
      location=Location, #Location could be a name or coordinates in english format : 4.04827,9.70428 
      keyword='', 
      radius=1000,#Radius in meters (max = 50 000) 
      types=[types.TYPE_PHARMACY] #Specify the Type 
      ) 

     for place in query_result.places: 
     # Returned places from a query are place summaries. 
      Name = place.name 
      Name = Name.encode('utf-8')#Encodage en UTF-8 obligatoire pour éviter les erreurs : "UnicodeEncodeError: 'ascii' codec can't encode character..." 
      Latitude = place.geo_location['lat'] 
      Longitude = place.geo_location['lng'] 
      # The following method has to make a further API call. 
      place.get_details() 
      Details = place.details 
      Address = place.formatted_address 
      Address = Address.encode('utf-8') 
      Phone_International = place.international_phone_number 
      Website = place.website 

     Result = str(MyType) + ";" + Name + ";" + Address + ";" + str(Phone_International) + ";" + str(Latitude) + ";" + str(Longitude) 
+ ";" + str(Website) 
     print Result 

#################################### 
### Script 
#################################### 

Location = "4.04827,9.70428" 
MyType = "DOES_NOT_WORK" 
ExtractGoogleHotspots(Location, MyType) 

Code, der nicht funktioniert:

# Configuration de l'encodage (a mettre dans tous les scripts) 
    # encoding: utf-8 

    #################################### 
    ### CLASS 
    #################################### 

def ExtractGoogleHotspots(Location,Type): 
      from googleplaces import GooglePlaces, types, lang 
      YOUR_API_KEY = 'XXXXXXX' 
      google_places = GooglePlaces(YOUR_API_KEY) 
      MyType = "[types.TYPE_"+Type+"]" 
      # You may prefer to use the text_search API, instead. 
      query_result = google_places.nearby_search(
       location=Location, #Location could be a name or coordinates in english format : 4.04827,9.70428 
       keyword='', 
       radius=1000,#Radius in meters (max = 50 000) 
       types=MyType #Specify the Type 
       ) 

      for place in query_result.places: 
      # Returned places from a query are place summaries. 
       Name = place.name 
       Name = Name.encode('utf-8')#Encodage en UTF-8 obligatoire pour éviter les erreurs : "UnicodeEncodeError: 'ascii' codec can't encode character..." 
       Latitude = place.geo_location['lat'] 
       Longitude = place.geo_location['lng'] 
       # The following method has to make a further API call. 
       place.get_details() 
       Details = place.details 
       Address = place.formatted_address 
       Address = Address.encode('utf-8') 
       Phone_International = place.international_phone_number 
       Website = place.website 

       Result = str(MyType) + ";" + Name + ";" + Address + ";" + str(Phone_International) + ";" + str(Latitude) + ";" + str(Longitude) 
     + ";" + str(Website) 
       print Result 

     #################################### 
     ### Script 
     #################################### 

     Location = "4.04827,9.70428" 
     MyType = "PHARMACY" 
     ExtractGoogleHotspots(Location, MyType) 

Wie das Problem zu lösen, eine Variable zu haben, die Typen zu definieren, Teil?

+1

Wenn Ihr bearbeiten beantwortet die Frage und der Code funktioniert, dann sollten Sie weiter unten schreiben, anstatt die Aktualisierung der Frage verwenden können. Jetzt ist nicht klar, was Sie fragen. –

Antwort

1

Um den Typnamen mit TYPE_ in take verketten es von types Sie getattr

MyTypes = [ getattr(types, 'TYPE_' + Type.upper() ] 
+0

Danke Marco. Ich habe versucht, aber ich habe den folgenden Fehler: UnboundLocalError: lokale Variable 'Typen' vor der Zuweisung referenziert – neonepthys

+0

@neonepthys importieren Sie die Google Bibliothek wie in Ihrem ersten Beispiel? – Marco

+0

Ich bin ein Neuling im Codieren und ich verstehe Ihren Standpunkt nicht. Ich habe den Code mit dem von Ihnen geposteten geändert und es hat nicht funktioniert. – neonepthys