close

可以使用的函數:

  • strip() ->去除字串兩邊的空白
  • lstrip()->去除字串左邊的空白
  • rstrip()->去除字串右邊的空白
  • replace(' ','')->第一個參數為要替代的字串,要去除空白所以就打一個空白字串
  • replace(u'\xa0','')->這個是比較特別的空白,通常是在網路爬蟲的時候,有&nbsp的html空格,用如上的方式會發現去不掉,所以必須在unicode下做替換使用\xa0

 

>>> temp = "  hello Kevin!! "
 
>>> temp.strip()
'hello Kevin!!'
 
>>> temp.lstrip()
'hello Kevin!! '
 
>>> temp.rstrip()
'  hello Kevin!!'

>>> temp.replace(' ','')
'helloKevin!!'

--------------------------
r = requests.get(url)
r.encoding = 'unicode'
soup = bf(r.text,'html.parser')
for n,i in enumerate(a.select('tr')[7:],0):
   temp = i.text.strip().replace(u'\xa0', '')
..
..
..
..
--------------------------

 

arrow
arrow
    文章標籤
    space 空格 python
    全站熱搜
    創作者介紹
    創作者 KV 的頭像
    KV

    kevin的部落格

    KV 發表在 痞客邦 留言(0) 人氣()