Question

I'm making an app that uses the Google Places API.

This is the code snippet where I'm building a string for types parameter in the URL.

url = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?"
    #required params
requiredparams = "location="+str(lat)+","+str(lon)+"&radius="+str(radius)+"&sensor=true&rankby=distance&types="

place_types = "bakery|bar|beauty_salon|book_store|bowling_alley|cafe|car_dealer|car_rental|car_wash|car_repair|\
    clothing_store|convenience_store|department_store|electronics_store|florist|food|furniture_store|\
    grocery_or_supermarket|gym|hair_care|hardware_store|health|home_goods_store|jewelry_store|laundry|liquor_store|\
    locksmith|meal_delivery|meal_takeaway|night_club|moving_company|pet_store|pharmacy|plumber|restaurant|shoe_store|\
    shopping_mall|spa|store|taxi_stand|travel_agency"

When I print it (url+requiredparams+place_types), I'm getting gaps before words that start on a new line.

https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=10,13&radius=500&sensor=true&rankby=distance&types=bakery|bar|beauty_salon|book_store|bowling_alley|cafe|car_dealer|car_rental|car_wash|car_repair|       clothing_store|convenience_store|department_store|electronics_store|florist|food|furniture_store|       grocery_or_supermarket|gym|hair_care|hardware_store|health|home_goods_st
ore|jewelry_store|laundry|liquor_store| locksmith|meal_delivery|meal_takeaway|night_club|moving_company|pet_store|pharmacy|plumber|restaurant|shoe_store|       shopping_mall|spa|store|taxi_stand|travel_agency

I don't get it. What am I doing wrong?

I tried this on the console:

>>> d = "word1|\
... word2|\
... word3"
>>> d
'word1|word2|word3'

That works fine. Why not my code snippet?

Was it helpful?

Solution 2

You could do something like this:

place_types = (
    "bakery|bar|beauty_salon|book_store|bowling_alley|cafe|car_dealer|car_rental|car_wash|car_repair|"
    "clothing_store|convenience_store|department_store|electronics_store|florist|food|furniture_store|"
    "grocery_or_supermarket|gym|hair_care|hardware_store|health|home_goods_store|jewelry_store|laundry|liquor_store|"
    "locksmith|meal_delivery|meal_takeaway|night_club|moving_company|pet_store|pharmacy|plumber|restaurant|shoe_store|"
    "shopping_mall|spa|store|taxi_stand|travel_agency"
)

OTHER TIPS

Is for the identation inside the string.

Your snippet fixed:

pt = "bakery|bar|beauty_salon|book_store|bowling_alley|cafe|car_dealer|car_rental|car_wash|car_repair|\
clothing_store|convenience_store|department_store|electronics_store|florist|food|furniture_store|\
grocery_or_supermarket|gym|hair_care|hardware_store|health|home_goods_store|jewelry_store|laundry|liquor_store|\
locksmith|meal_delivery|meal_takeaway|night_club|moving_company|pet_store|pharmacy|plumber|restaurant|shoe_store|\
shopping_mall|spa|store|taxi_stand|travel_agency"

or better user multiline strings """:

pt = """bakery|bar|beauty_salon|book_store|bowling_alley|cafe|car_dealer|car_rental|car_wash|car_repair|
clothing_store|convenience_store|department_store|electronics_store|florist|food|furniture_store|
grocery_or_supermarket|gym|hair_care|hardware_store|health|home_goods_store|jewelry_store|laundry|liquor_store|
locksmith|meal_delivery|meal_takeaway|night_club|moving_company|pet_store|pharmacy|plumber|restaurant|shoe_store|
shopping_mall|spa|store|taxi_stand|travel_agency"""

Because your string looks like "bakery|bar|beauty_salon|book_store|bowling_alley|cafe|car_dealer|car_rental|car_wash|car_repair|\ clothing_store|convenience_store|department_store|electronics_store|florist|food|furniture_store|\ (without any line break) to Python. Don't use indention or maybe concat. your string could solve that issue.

Try using only one newline between lines, looks like you have one extra (on my phone at least). Avoid extra white space in general after the backslash + newline.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top