Python,html和写入文本文件

时间:2011-12-02 18:20:50

标签: python

我正在创建一个html字符串并将其写入.html文件。我用json和xml从网站上提取数据。这会拉动文本和链接,并将它们放入三重引号字符串中。这个html字符串还有更多,但这将显示问题。我有一个函数,它接受新闻字符串并将字符串写入.html文件。我知道数据的所有插入都有效,因为在我将字符串发送到写入文件函数之前,我将其打印到屏幕并将其复制到文本文件并在浏览器上显示并正确显示所有正确的数据数据。 python写入文件时出现问题。我得到一个“必须是字符串错误”。

当唯一的数据是文本时写入文件就好了,但是一旦它开始成为url就会无法写入。好吧,每次写作都不会写,但一次就足够了。

我有一个理论。有些网址包含转义字符或某些内容(编程时间太长)。或者我插入的数据在插入html字符串之前需要放在三引号中。我不确定这是否可能。

我尝试使用以下方法插入数据:news%(数据,数据,数据),但它无法解决问题。

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>""")

1 个答案:

答案 0 :(得分:2)

我不能肯定地说没有看到输入数据,但可能是因为其中一些是数字(intfloat)或可能是unicode(这会产生一个数字) UnicodeEncodeError)。

最简单的方法是将所有变量包装在str(…)unicode(…)中,但如果是我编写此代码,我会安装Jinja2并使用“真正的”HTML模板语言(它将具有循环和内容,这将使HTML模板更易于阅读)。