当遍布多行时,Python字符串未按预期格式化

时间:2014-03-20 08:49:19

标签: python newline

我正在制作一款使用Google Places API的应用。

这是我在URL中为types参数构建字符串的代码段。

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"

当我打印它(url+requiredparams+place_types)时,我会在开始新行的单词之前得到空白。

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

我不明白。我做错了什么?

我在控制台上尝试了这个:

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

工作正常。为什么不是我的代码片段?

4 个答案:

答案 0 :(得分:1)

是否为字符串内的缩进。

您的代码片段已修复:

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"

或更好的用户多行字符串"""

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"""

答案 1 :(得分:0)

因为你的字符串看起来像"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|\(没有任何换行符)到Python。不要使用缩进或者连续使用。你的字符串可以解决这个问题。

答案 2 :(得分:0)

尝试在两行之间只使用一个换行符,看起来你有一个额外的(至少在我的手机上)。反斜杠+换行符后,一般不要使用额外的空格。

答案 3 :(得分:0)

你可以这样做:

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