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?

有帮助吗?

解决方案 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"
)

其他提示

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top