Python字符串在跨多行传播时未按预期格式化

Python字符串在跨多行传播时未按预期格式化,python,newline,Python,Newline,我正在制作一个使用GooglePlacesAPI的应用程序 这是我为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&ra

我正在制作一个使用GooglePlacesAPI的应用程序

这是我为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+requiredparms+place\u 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'

那很好。为什么不是我的代码片段?

因为你的字符串看起来像
“面包房、酒吧、美容沙龙、书店、保龄球馆、咖啡馆、汽车经销商、租车、洗车、修车、服装店、便利店、百货店、电子店、花店、食品店、家具店”(没有任何代码)对于Python。不要使用缩进或concat。您的字符串可以解决这个问题。

尝试在行与行之间只使用一个换行符,看起来您有一个额外的换行符(至少在我的手机上)。通常避免在反斜杠+换行符后使用额外的空格。

用于字符串内的标识

您的代码片段已修复:

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"
或更好的用户多行字符串
“”


你可以这样做:

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

我用的是Supreme Text2。在
place\u types`的每一行末尾添加一个
`后,我只需按enter键,缩进就会自动完成。我确实删除了标签,然后让新行从头开始,但仍然有空格。像这样,我仍然会在新行开头的单词前面看到
\t
。这在很多方面都很难看。。。与其硬编码字符串,不如将单个单词放在列表中,然后执行“连接(单词)”
?@Bakuriugreat idea!谢谢我是Python新手。没有想到这一点;看起来也很整洁,很有条理。嗨!谢谢这就像魔术一样奏效。读起来很简单。是的。虽然实际上我是按照@Bakuriu的建议来做的,但由于他的建议是一个评论,而你的建议仍然是一个很好的答案,所以我接受这个建议。
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"
)