2015-06-27 47 views
1

我正在用Python和燒瓶製作網站,並在我的HTML表格中打印: Rory O\u0027Shea Was Here而不是Rory O'Shea Was Here我的HTML表打印 u0027而不是撇號

當我在終端上運行Python程序時,它會打印出來很好,但是當我將它放到我的HTML模板中時,它會打印出\u0027

我怎樣才能打印預期的Rory O'Shea Was Here

這裏是main.py(運行燒瓶應用):

from flask import Flask, render_template, request 
from hboA import HbobyRating 

app = Flask(__name__) 

@app.route("/hbo") 
def hbo(): 
    name = "HBO" 
    logo = "/static/pics/hbo-logo.jpg" 
    hbomovies = HbobyRating 
    db = hbomovies 
    return render_template('channel_template.html', hbomovies=hbomovies, name=name, db=db, logo=logo) 

這裏是imdbHbo.py(這產生使用IMDB API電影數據庫):

import json 
import urllib2 


with open('/home/chim/Documents/moveit/hbomovies.json') as data_file: #opens the movie database from Guidebox api 
    moviedata = json.load(data_file) 

Id = [x['imdb'] for x in moviedata['results']]    #extracts the imdbid from guidebox api 

pyList = [] 
def getMovieRating():       #creates a new json file containing the imdb data from the channels of guidebox api 
    for i in Id: 
     imdbid = i  
     imdbapi = "http://www.omdbapi.com/?i=%s&plot=short&r=json" % imdbid 
     response = urllib2.urlopen(imdbapi) 
     result = json.load(response) 
     pyList.append(result) 
     with open('/home/chim/Documents/moveit/static/hboImdb.json', 'w') as outfile: 
       json.dump(pyList, outfile) 

getMovieRating() 

這裏是hboA.py python(這種將電影按其IMDB評級排列成列表):

import json 


with open('/home/chim/Documents/moveit/final/hboImdb.json') as data_file: #opens the imdb database we created 
    response = json.load(data_file) 


def getKeyRat(item): 
    return item[1] 

def getKeyAlpha(item): 
    return item[0] 



TitleRating = [[x["Title"].encode('utf-8'), x['imdbRating'], x['Year'], x['Rated'], x['Genre'], x['Runtime']] for x in response] 


for each in TitleRating: 
    each.append("HBO") 


HbobyRating = sorted(TitleRating, key=getKeyRat, reverse=True) 
HbobyAlpha = sorted(TitleRating, key=getKeyAlpha) 

這裏是HTML:

<!DOCTYPE html> 


<img src={{logo}} alt="logo" style="width:512px;height:320px;" align="right"> 
<head> 
    <title>Welcome to MoveIt</title> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
</head> 

<h1> Welcome to MoveIt</h1> 

<p>check out HBO <a href="/hbo">click here</a></p> 
<p>check out Cinemax <a href="/cinemax">click here</a></p> 
<p>check out Showtime <a href="/showtime">click here</a></p> 

<p> Or see a list by channel</p> 
<form action="/select" method="POST"> 
<fieldset> 
    <input type="checkbox" name="channels" value="hbomovies" />HBO<br> 
    <input type="checkbox" name="channels" value="cinemaxmovies" />Cinemax<br> 
    <input type="checkbox" name="channels" value="showtimemovies" />Showtime<br> 
    <input type="submit" value="Submit"> 
</fieldset> 
</form> 


<h2>These are the movies on {{name}} <i>right now</i>, listed by their IMDB rating:</h2> 
<table border="1"> 
<tr> 
    <th>Title</th> 
    <th>Imdb Rating</th>   
    <th>Year</th> 
    <th>Rated</th> 
    <th>Genre</th>  
    <th>Runtime</th> 
    </tr> 
{% for row in db %} 
    <tr> 
     <td>{{ row[0]|tojson|safe }}</td> 
     <td>{{ row[1] }}</td> 
     <td>{{ row[2] }}</td> 
     <td>{{ row[3] }}</td> 
     <td>{{ row[4] }}</td> 
     <td>{{ row[5] }}</td> 
    </tr> 
{% endfor %} 
</table> 
+1

你如何將這個字符串傳遞給你的模板?它來自哪裏?數據庫?用戶輸入? – IanAuld

+0

它來自一個json文件,它被解析成一個列表到我的python文件中。 –

+0

你在哪裏編寫你的html?顯示代碼。 – Daniel

回答

1

你在不同的地方有你的代碼的問題。

首先,爲什麼用utf-8編碼Title?內部字符串必須使用Unicode,只寫入一個流時,編碼是必要的:

import json 
from operator import attrgetter 

DATABASE = '/home/chim/Documents/moveit/final/hboImdb.json' 

def read_database(database): 
    with open(database) as data_file: #opens the imdb database we created 
     response = json.load(data_file) 

    hbo_by_rating = sorted(response, key=attrgetter('imdbRating'), reverse=True) 
    hbo_by_alpha = sorted(response, key=attrgetter('Title')) 
    return hbo_by_rating, hbo_by_alpha 

hbo_by_rating, hbo_by_alpha = read_database(DATABASE) 

接下來,在你的模板,你說明確「的toJSON」的逃逸每個非ASCII字符爲Unicode \uxxxx。只需刪除它:

<h2>These are the movies on {{name}} <i>right now</i>, listed by their IMDB rating:</h2> 
<table border="1"> 
<tr> 
    <th>Title</th> 
    <th>Imdb Rating</th>   
    <th>Year</th> 
    <th>Rated</th> 
    <th>Genre</th>  
    <th>Runtime</th> 
    </tr> 
{% for row in db %} 
    <tr> 
     <td>{{ row.Title }}</td> 
     <td>{{ row.imdbRating }}</td> 
     <td>{{ row.Year }}</td> 
     <td>{{ row.Rated }}</td> 
     <td>{{ row.Genre }}</td> 
     <td>{{ row.Runtime }}</td> 
    </tr> 
{% endfor %} 
</table> 
+0

這樣做了!這是'tojson'的東西,它搞砸了,我只是刪除它。多謝你們! –

0

您需要在上傳之前對其進行編碼。使用foo.encode('utf-8')其中foo是包含字符串的變量。

+0

當我這樣做時,它將終端中的python輸出更改爲:「Rory O'Shea在這裏」而不是「u'Rory O'Shea在這裏」,但html看起來一樣... –