[英]data dictionary to html table
I have the following data dictionary, I would like to convert this to HTML table - tried different things but no success. 我有以下数据字典,我想将其转换为HTML表-尝试了不同的方法,但没有成功。
data = {'102 Not Out': "('75', '04 May 2018'),N/A",'2.0': "('84', 'Expected in January 2019'),8.5",'3 Dev': "('0', '11 May 2018'),No IMDB Info Available",'Adityam': "('34', '27 April 2018'),No IMDB Info Available"}
Here is the what I have so far 这是我到目前为止所拥有的
message ='<table><tr><th>Movies</th><th>Release Date</th><th>Rating</th><th>IMDB Rating</th></tr>'
message += '<tr><td>' + '</td><td>'.join(data.keys()) + '</td></tr>'
message += '</table>'
Here is the output I am looking for: 这是我正在寻找的输出:
+-------------+--------------------------+--------+------------------------+--+
| Movies | Release Date | Rating | IMDB Rating | |
+-------------+--------------------------+--------+------------------------+--+
| 102 Not Out | 04 May 2018 | 75 | N/A | |
+-------------+--------------------------+--------+------------------------+--+
| 2.0 | Expected in January 2019 | 84 | 8.5 | |
+-------------+--------------------------+--------+------------------------+--+
| 3 Dev | 11 May 2018 | 0 | No IMDB Info Available | |
+-------------+--------------------------+--------+------------------------+--+
| Adityam | 27 April 2018 | 34 | No IMDB Info Available | |
+-------------+--------------------------+--------+------------------------+--+
Using Python 3 - literal_eval()
and .items()
使用Python 3 -
literal_eval()
和.items()
Your value contains a tuple
and a str
. 您的值包含一个
tuple
和一个str
。 So you need to split it. 因此,您需要将其拆分。
So should be like: 所以应该像:
from ast import literal_eval
for key, value in data.items():
message += '<tr>'
values = value.split(',')
t = literal_eval(','.join(values[:2])) # tuple
message += """
<td>{}</td><td>{}</td><td>{}</td><td>{}</td>
""".format(key, t[0], t[1], values[2])
message += '</tr>'
102 Not Out 75 04 May 2018 N/A
2.0 84 Expected in January 2019 8.5
3 Dev 0 11 May 2018 No IMDB Info Available
Adityam 34 27 April 2018 No IMDB Info Available
You can use re
and dict.items
: 您可以使用
re
和dict.items
:
import re
import ast
message ='<table><tr><th>Movies</th><th>Release Date</th><th>Rating</th><th>IMDB Rating</th></tr>{}</table>'
data = {'102 Not Out': "('75', '04 May 2018'),N/A",'2.0': "('84', 'Expected in January 2019'),8.5",'3 Dev': "('0', '11 May 2018'),No IMDB Info Available",'Adityam': "('34', '27 April 2018'),No IMDB Info Available"}
new_data = '\n'.join('<tr>{}</tr>'.format('\n'.join(c.format(d) for c, d in zip(['<td>{}</td>']*4, [a]+list(ast.literal_eval(re.findall('\(.*?\)', b)[0])[::-1])+[re.findall('(?=\),)[\w\W]+', b)[0][2:]]))) for a, b in data.items())
last_result = message.format(new_data)
with open('movie_listings.html', 'w') as f:
f.write(last_result)
Output: 输出:
However, this task is much easier with flask
. 但是,使用
flask
可以轻松完成此任务。 First, create a class to better structure data
: 首先,创建一个类以更好地构造
data
:
import ast
import re
from collections import namedtuple
class MovieData:
def __init__(self, data):
self.final_data = [[a]+list(ast.literal_eval(re.findall('\(.*?\)', b)[0])[::-1])+[re.findall('(?=\),)[\w\W]+', b)[0][2:]])) for a, b in data.items()]
def __iter__(self):
movie = namedtuple('movie', ['name', 'release', 'rating', 'imbd'])
for i in self.final_data:
yield movie(*i)
Then, create the template in HTML ( movie_listing.html
): 然后,以HTML(
movie_listing.html
)创建模板:
<html>
<body>
<table>
<thead>
<tr>
<th>Movies</th>
<th>Release Date</th>
<th>Rating</th>
<th scope="col">IMDB Rating</th>
</tr>
</thead>
<tbody>
{%for movie in movie_data%}
<tr>
<td>{{movie.name}}</td>
<td>{{movie.release}}</td>
<td>{{movie.rating}}</td>
<td>{{movie.imbd}}</td>
</tr>
{%endfor%}
</tbody>
</table>
</body>
</html>
Finally, create and run the app: 最后,创建并运行该应用程序:
import flask
app = flask.Flask(__name__)
@app.route('/')
def get_html():
data = {'Adityam': "('34', '27 April 2018'),No IMDB Info Available", '3 Dev': "('0', '11 May 2018'),No IMDB Info Available", '102 Not Out': "('75', '04 May 2018'),N/A", '2.0': "('84', 'Expected in January 2019'),8.5"}
return flask.render_template('movie_listing.html', movie_data = MovieData(data))
if __name__ == '__main__':
app.debug = True
app.run()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.