繁体   English   中英

Python-串联Python查询

[英]Python - Concatenate Python query

我正在使用python-google-places脚本,但是将代码集成到带有两个参数调用的函数中时遇到问题:“ MyLocation”和“ MyType”。

“ MyLocation”工作正常,但“ MyType”无效。

有效的代码(“ MyType”没有变量)

# 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)

无效的代码:

    # 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)

如何解决使用变量定义Types部分的问题?

与串联类型名称TYPE_在把它从types ,你可以使用getattr

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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