Python中字符串String去除出換行符和空格的問(wèn)題(\n,\r)
在Python的編寫(xiě)過(guò)程中,獲取到的字符串進(jìn)場(chǎng)存在不明原因的換行和空格,如何整合成一個(gè)單句,成為問(wèn)題。
方法:
一、去除空格
“·”代表的為空格
strip()
"···xyz···".strip()#returns"xyz"
"···xyz···".lstrip()#returns"xyz···"
"···xyz···".rstrip()#returns"···xyz"
"··x·y·z··".replace('','')#returns"xyz"
二、替換replace(“space”,"")
用replace("\n",“”),與replace("\r",“”),后邊的內(nèi)容替換掉前邊的。
以上內(nèi)容為大家介紹了python如何刪除文本中的換行符,希望對(duì)大家有所幫助,如果想要了解更多Python相關(guān)知識(shí),請(qǐng)關(guān)注IT培訓(xùn)機(jī)構(gòu):千鋒教育。