I'm trying to get some strings using SQLAlchemy.
tags = db.session.query(Tag.tag_name).order_by(Tag.tag_id).all()
db
is my sqlite database. tag_name
entities are VARCHAR
.
When I'm debugging the program, it shows the tags variable is
{list}[(u'python',),(u'\u7f51',)]
Here \网
is a Chinese character.
As the project is based on Flask
framework, I use Jinja2
to show each tag in tags
.
{% for tag in tags %}
<li><a href="#">{{tag}}</a></li>
{% endfor %}
On the web page it shows exactly like (u'python',)
. I want the web page to show things like python
, but I don't know how to deal with this strange list object. What should I do?
This should be very straight forward. The query result set is obtained in tuple
format. So you have a list of tuples. And for non-utf8 characters use encode
function and use list comprehension to generate a list of strings.
You can do this:
tags = [r(0).encode('utf8') for r in tags]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.