2011-12-02 85 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