我想在scrapy的文本輸出中添加一些字符。
這是我的代碼:
thumbnailname = detail.xpath('.//h1/text()').get().replace(' ','-')
這是我的文本輸出:
蘋果-iPhone-13-Pro-Max
如何獲得以下輸出?
My-Apple-iPhone-13-Pro-Max.jpg
請注意,文本應插入到開頭和結尾。
uj5u.com熱心網友回復:
只需連接字串:
thumbnailname = "My-" detail.xpath('.//h1/text()').get().replace(' ','-') ".jpg"
uj5u.com熱心網友回復:
您有兩種簡單的方法可以在字串之前和之后添加字串:
yourstring = "start{0}end".format(yourstring)yourstring = "start" yourstring "end"
在你的情況下:
thumbnailname = detail.xpath('.//h1/text()').get().replace(' ','-')
thumbnailname = "My-" thumbnailname ".jpg"
或者
thumbnailname = "My-{0}.jpg".format(detail.xpath('.//h1/text()').get().replace(' ','-'))"
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/420086.html
標籤:
