2011-12-02 126 views
-1

我正在創建一個html字符串並將其寫入一個.html文件。我用json和xml從網站上提取數據。這將拉動文本和鏈接,並將它們放入三重引號字符串中。還有更多的這個HTML字符串,但這很多將顯示問題。我有一個函數獲取新聞字符串並將該字符串寫入.html文件。我知道所有的數據插入的作品,因爲在我發送字符串寫入文件函數之前,我已經打印到屏幕上並將其複製到文本文件並顯示在瀏覽器上,並且它正確顯示所有正確的數據。 python寫入文件時發生問題。我得到一個「必須是字符串錯誤」。Python,html和寫入文本文件

當只有數據是文本時它才寫入文件,但一旦它開始成爲urls它將無法寫入。那麼,它不會寫每一次,但一旦足夠。

我有一個理論。一些網址包含轉義字符或其他一些(未編程時間過長)。或者,也許我插入的數據在插入到html字符串之前需要使用三重引號。我不確定這甚至是可能的。

我嘗試過使用這個插入數據:新聞%(數據,數據,數據),但它不能解決問題。

news = ("""<div id='weather'>""" + 
    """<center><h1>Weather</h1></center>""" + 
    """<center><cite>Weatherunderground</cite></center>""" +   
    """<hr />""" + 
    """<h6>""" + weather['forecast']['txt_forecast']['forecastday'][0]['title'] + """</h6>""" + 
    """<p>""" + weather['forecast']['txt_forecast']['forecastday'][0]['fcttext'] + """</p>""" + 
    """<h6>Forecast</h6>""" 
    """<table border='1'>""" 
    """<tr>""" 
    """<th style='font-size: small;'>""" + weather['forecast']['simpleforecast']['forecastday'][0]['date']['weekday'] + """</th>""" + 
    """<th style='font-size: small;'>""" + weather['forecast']['simpleforecast']['forecastday'][1]['date']['weekday'] + """</th>""" + 
    """<th style='font-size: small;'>""" + weather['forecast']['simpleforecast']['forecastday'][2]['date']['weekday'] + """</th>""" + 
    """<th style='font-size: small;'>""" + weather['forecast']['simpleforecast']['forecastday'][3]['date']['weekday'] + """</th>""" + 
    """</tr>""" + 
    """<tr>""" + 
    """<td style='font-size: small;'>""" + weather['forecast']['simpleforecast']['forecastday'][0]['conditions'] + """</td>""" + 
    """<td style='font-size: small;'>""" + weather['forecast']['simpleforecast']['forecastday'][0]['conditions'] + """</td>""" + 
    """<td style='font-size: small;'>""" + weather['forecast']['simpleforecast']['forecastday'][0]['conditions'] + """</td>""" + 
    """<td style='font-size: small;'>""" + weather['forecast']['simpleforecast']['forecastday'][0]['conditions'] + """</td>""" + 
    """</tr>""" + 
    """<tr>""" + 
    """<td style='font-size: small;'>high """ + weather['forecast']['simpleforecast']['forecastday'][0]['high']['fahrenheit'] + """</td>""" + 
    """<td style='font-size: small;'>high """ + weather['forecast']['simpleforecast']['forecastday'][0]['high']['fahrenheit'] + """</td>""" + 
    """<td style='font-size: small;'>high """ + weather['forecast']['simpleforecast']['forecastday'][0]['high']['fahrenheit'] + """</td>""" + 
    """<td style='font-size: small;'>high """ + weather['forecast']['simpleforecast']['forecastday'][0]['high']['fahrenheit'] + """</td>""" + 
    """</tr>""" + 
    """<tr>""" + 
    """<td style='font-size: small;'>low """ + weather['forecast']['simpleforecast']['forecastday'][0]['low']['fahrenheit'] + """</td>""" + 
    """<td style='font-size: small;'>low """ + weather['forecast']['simpleforecast']['forecastday'][0]['low']['fahrenheit'] + """</td>""" + 
    """<td style='font-size: small;'>low """ + weather['forecast']['simpleforecast']['forecastday'][0]['low']['fahrenheit'] + """</td>""" + 
    """<td style='font-size: small;'>low """ + weather['forecast']['simpleforecast']['forecastday'][0]['low']['fahrenheit'] + """</td>""" + 
    """</tr>""" + 
    """</table>""" + 
    """</div>""" + 
    """<hr />""" + 
    """<div id='kindle'>""" + 
    """<center><h1>Amazon Daily Kindle Deal</h1></center>""" + 
    """<center><cite>amazon</cite></center>""" + 
    """<hr />""" + 
    """<div class='firstHeadline'>""" + 
    """<h3>""" + kind[0][0] + """</h3>""" + 
    """<p class='content'>""" + kind[1][0] + """</p>""" + 
    """<p class='url'><a href='""" + kind[2][0] + """'>""" + kind[2][0] + """</a></p>""" + 
    """</div>""" + 
    """<hr />""" + 
    """</div>""" + 
    """<div id='amazonApp'>""" + 
    """<center><h1>Amazon App of the Day</h1></center>""" + 
    """<center><cite>amazon</cite></center>""" + 
    """<hr />""" + 
    """<div class='firstHeadline'>""" + 
    """<h3>""" + app[0][0] + """</h3>""" + 
    """<p class='content'>""" + app[1][0] + """</p>""" + 
    """<p class='url'><a href='""" + app[2][0] + """'>""" + app[2][0] + """</a></p>""" + 
    """</div>""" + 
    """<hr />""" + 
    """</div>""") 
+0

你有什麼錯誤嗎?如果是這樣,你可以發佈他們嗎? – Blender

+0

我收到一個錯誤:UnicodeEncodeError:'ascii'編解碼器無法編碼字符u'\ xbf'在位置3471:序號不在範圍內(128) – Derek

+0

感嘆。這是一個常見問題。搜索「UnicodeEncodeError ascii編解碼器」。閱讀:http://docs.python.org/howto/unicode.html –

回答

2

我不能肯定地說,沒有看到輸入數據,但很可能是因爲他們中的一些數字(intfloat)或可能unicode(這將產生UnicodeEncodeError)。

做將是包裝在str(…)unicode(…)所有變量的最簡單的事情,但如果是我寫這個代碼,我會安裝Jinja2並做到這一點使用一個「真正的」 HTML模板語言(這將有環和東西,這將使HTML模板更容易閱讀)。

+0

我試着在字符串中包裝變量,並得到這個錯誤:UnicodeEncodeError:'ascii'編解碼器無法編碼字符u'\ xbf'在位置3471:序號不在範圍(128)。真的希望我現在可以用jinja2來看看它。可能會切換,但仍然對這個問題感到好奇。對於Jinja2,爲 – Derek

+0

+1。如果你正在處理像這樣的一個以上的情況,一個真正的模板引擎將爲你節省大量的時間。 – Wilduck