2011-12-02 5 views
-1

나는 html 문자열을 만들고 .html 파일에 쓰려고합니다. json과 xml이있는 웹 사이트에서 데이터를 가져옵니다. 이렇게하면 텍스트와 링크를 가져와 삼중 따옴표로 묶은 문자열에 배치합니다. 이 html 문자열에 더 많은 것이 있지만이 정도만 문제를 보여줄 것입니다. 뉴스 문자열을 받아 .html 파일에 문자열을 쓰는 함수가 있습니다. 나는 데이터를 삽입하는 모든 기능을 작동하기 전에 문자열을 파일에 쓰기 기능에 보내기 전에 화면에 인쇄하고 텍스트 파일에 복사 한 다음 브라우저에 표시했기 때문에 올바른 모든 문자가 올바르게 표시되기 때문에 작동한다는 것을 알고 있습니다. 데이터. 이 문제는 파이썬이 파일에 쓸 때 발생합니다. 나는 "문자열 오류가 있어야한다"고합니다.Python, html 및 텍스트 파일에 쓰기

유일한 데이터가 텍스트 일 ​​때 파일에 쓰기가되었지만 일단 URL이되기 시작하면 쓰기가 실패합니다. 글쎄, 매번 글을 쓰지는 않았지만 한번이면 충분합니다.

나는 이론을 가지고있다. 일부 URL에는 이스케이프 문자가 포함되어 있거나 너무 길게 프로그래밍되지 않았습니다. 또는 삽입 할 데이터를 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'코덱이 3471 위치의 문자 '\ xbf'를 인코딩 할 수 없습니다. 서수가 범위에 없음 (128) – Derek

+0

Sigh. 이것은 FAQ입니다. "UnicodeEncodeError ascii codec"을 검색하십시오. 읽기 : http://docs.python.org/howto/unicode.html –

답변

2

I는 입력 데이터를 보지 않고 확실히 말할 수는 없지만, 그들 중 일부는 숫자 있기 때문에 가능성 (intfloat) 또는 가능 unicode (A UnicodeEncodeError를 얻을 것이다).

str(…) 또는 unicode(…)에있는 모든 변수를 포장하는 것 가장 간단한 것,하지만 날이 코드를 작성한다면, 나는 Jinja2를 설치하고 루프를 가질 것이다 ("진짜"HTML의 템플릿 언어를 사용하여이 작업을 수행 할 것 HTML 템플릿을 더 쉽게 읽을 수있게 해줍니다).

+0

문자열에서 변수를 래핑하려고 시도했는데이 오류가 발생했습니다. UnicodeEncodeError : 'ascii'코덱이 3471 위치의 문자 '\ xbf'를 인코딩 할 수 없습니다. 서수가 범위에 없습니다 (128). 정말로 내가 그것을 보았을 때 나는 진쟈 2를 사용했을 것이다. 전환 할 수 있지만 여전히이 문제에 궁금해합니다. Jinja2의 경우 – Derek

+0

+1입니다. 이와 같은 상황을 두 가지 이상 처리하는 경우 실제 템플릿 엔진을 사용하면 많은 시간을 절약 할 수 있습니다. – Wilduck