gpt4 book ai didi

python - 我的 HTML 表格打印\u0027 而不是撇号

转载 作者:太空宇宙 更新时间:2023-11-03 17:36:55 25 4
gpt4 key购买 nike

我正在使用 Python 和 Flask 制作一个网站,并在我的 HTML 表中打印:Rory O\u0027Shea Was Here 而不是 Rory O'Shea Was Here

当我在终端中运行 Python 程序时,它会打印得很好,但是当我将它放入 HTML 模板时似乎出现了问题,这使得它打印 \u0027

如何让它打印预期的Rory O'Shea Was Here

这是 main.py(它运行 Flask 应用程序):

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>

最佳答案

您的代码在不同地方遇到问题。

首先,为什么Title采用utf-8编码?内部字符串必须使用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>

关于python - 我的 HTML 表格打印\u0027 而不是撇号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31093272/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com