[問題] 空白消除不掉?

看板Python作者 (名器)時間9年前 (2016/04/03 17:02), 編輯推噓0(004)
留言4則, 3人參與, 最新討論串1/1
大家好:D python自學新手,遇到問題想請教一下 from bs4 import BeautifulSoup as bs import requests req = requests.get('http://mops.twse.com.tw/mops/web/t164sb04?encodeURIComponent=1&s        tep=1&firstin=1&off=1&keyword4=&code1=&TYPEK2=&checkbtn=&queryNa        me=co_id&TYPEK=all&isnew=true&co_id=2330&year=104&season=04') req.encoding ='utf-8' soup =bs(req.text,'html5lib') table = soup.find('table', {'class': 'hasBorder'}) title =[] num =[] #print (table) for tr in table.select('tr') : if len (tr.select('td')) >0: sub = (tr.select('td')[0].text) subb=sub.replace('\u3000', '') title.append(subb) numm= str((tr.select('td')[1].text)) numm.replace(' ','') num.append(numm) print (num) ---------------------------------- result: [' 843,497,368', ' 843,497,368', ' 433,117,601', ' 410,379,767', ....以下省略...] 我num print出來數字前面都有空白 可是我replcae都取代不掉... 我知道這樣就是代表說那些不是空白...可是我也不知道那是甚麼 lol 想請問版友有甚麼方法可以刪除掉前面的空白 謝謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 1.174.208.10 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1459674152.A.B4E.html

04/03 17:09, , 1F
string 的 strip() method?
04/03 17:09, 1F

04/03 17:19, , 2F
我發現要另外用var = numm.replace() 再print就行了lol
04/03 17:19, 2F

04/03 17:20, , 3F
抱歉耍白癡lol
04/03 17:20, 3F

04/05 23:43, , 4F
replace 要 assign 回去原本的變數哦
04/05 23:43, 4F
文章代碼(AID): #1N0DmejE (Python)
文章代碼(AID): #1N0DmejE (Python)