2013-08-28 34 views
0

所以我一直在試圖格式化從CL拍攝網頁,以便我可以將它發送到我的郵箱, 但是這是我想出每次我嘗試任何事情,除去\n\t無法刪除python3字符串中的 n和 t?

b'\n\n\n\t\n\t\n\t\n\t\n\t\n\t\n\n\n\n\t\n\n\n\t 
\n\t\t\t 
\n\t 
\n\t\t 
\n\t\t\t 
\n 0 favorites\n 
\n\n\t\t 
\n\t\t 
∨ 
\n\t\t 
∧ 
\n\t\t 
\n \n 
\n 
\n\t \tCL wenatchee all personals casual encounters\n 
\n 
\n\t\t 
\n\t 
\n 
\n\n\t\t 
\n\t\t\t 
\n\t\n\t\t\n\t\n\n\n\nReply to: [email protected]\n 
\n\n\n\t 
\n\t\n\t\tflag [?] :\n\t\t\n\t\t\tmiscategorized\n\t\t\n\t\t\tprohibited\n\t\t\n\t\t\tspam\n\t\t\n\t\t\tbest of\n\t\n 
\n\n\t\t 

Posted: 2013-08-28, 8:23AM PDT 
\n 
\n\n 
\n \n Well... - w4m - 22 (Wenatchee)\n 

我時間已經試過剝離,替換,甚至正則表達式,但沒有任何事情,它總是出現在我的電子郵件不受任何事情影響。

下面的代碼:

try: 
    if url.find('http://') == -1: 
     url = 'http://wenatchee.craigslist.org' + url 
    html = urlopen(url).read() 
    html = str(html) 
    html = re.sub('\s+',' ', html) 
    print(html) 
    part2 = MIMEText(html, 'html') 
    msg.attach(part2) 
    s = smtplib.SMTP('localhost') 
    s.sendmail(me, you, msg.as_string()) 
    s.quit() 
+0

此代碼不會運行,您的文章實際上沒有格式化。格式化您的問題,併發佈一個[簡短,自包含的示例](http://sscce.org/),我們可以複製和粘貼以重現您的問題,否則您不可能獲得任何幫助。 –

回答

5

你的問題是,儘管所有證據相反,你仍然有bytes對象,而不是你希望的str。因此,你的嘗試沒有任何意義,因爲沒有指定編碼,就沒有辦法將任何東西(正則表達式,替換參數等)匹配到你的字符串中。

你需要做的是decode字節在先。

親自,我最喜歡清理空白的方法是使用string.splitstring.join。這是一個工作示例。我刪除了任何類型空白的所有運行,並將它們替換爲單個空格。

try: 
    html = urlopen('http://wenatchee.craigslist.org').read() 
    html = html.decode("utf-8") # Decode the bytes into a useful string 
    # Now split the string over all whitespace, then join it together again. 
    html = ' '.join(html.split()) 
    print(html) 
    s.quit() 
except Exception as e: 
    print(e) 
+0

是的,這工作!我不知道它仍然是編碼,那是什麼讓我絆倒! – user2727244

+0

@亨利非常感謝。 – Spiderman

+0

這個答案太棒了 –