Question

I have a python list which holds a few email ids accepted as unicode strings:

[u'one@example.com',u'two@example.com',u'three@example.com']

This is assigned to values['Emails'] and values is passed to render as html. The Html renders as this:

Emails: [u'one@example.com',u'two@example.com',u'three@example.com']

I would like it to look like this:

Emails: [ one@example.com, two@example.com, three@example.com ]

How can I do this in the server script? Is there a simple way to achieve this in the HTML itself?

Was it helpful?

Solution

The easiest way for you to do it would be to iterate over your email list. e.g.

{% for email in Emails %}
email,
{% endfor %}

This way you (or a designer) will have a lot more control of the layout.

Have a look at the template documentation for more details.

OTHER TIPS

In Python:

'[%s]' % ', '.join(pythonlistwithemails)

In bare HTML it is impossible... you'd have to use javascript.

I don't know any Python, but if those u-markers and the single quotes show, doesn't that actually indicate that you're accessing the list members in the wrong way?

You're printing the whole list rather than each item, and the output looks like debug information to me. Such debug information could very well look different in another version or configuration of Python. So, though I don't know Python, I'd say: you should NOT try to parse that.

Using liori's answer does not actually drop the u-markers, but ensures the items from the list are accessed individually, which gives you the true value rather than some debug information. You could also use some loop to achieve the same (though the join makes the code a lot easier).

"[{0}]".format(", ".join(python_email_list))

From Python 2.6 format() method is the preferred way of string formatting. Docs here.

It all depends how the HTML generation is treating your values array. Here is what you usually do to serialize a unicode string:

In [550]: ss=u'one@example.com'

In [551]: print ss   # uses str() implicitly
Out[552]: one@example.com

In [552]: str(ss)
Out[552]: 'one@example.com'

In [553]: repr(ss)
Out[553]: "u'one@example.com'"

If you are confident values only contains ASCII character strings, just use str() on the values. If you are unsure, use an explicit encoding like

ss.encode('ascii', error='replace')

In the template

[ {{ email_list|join:", " }} ]

note: the [, and ] are literal square brackets, not code :)

Option 1:

myStr = str('[ ' + ', '.join(ss) + ' ]')

Option 2:

myStr = '[ ' + ', '.join(ss) + ' ]'
myStr = myStr.encode(<whatever encoding you want>, <whatever way of handling errors you wish to use>)

I'm not used to Python pre-version 3, so I'm not really sure whether the additional strings need a u prefix or if it's an implicit conversion.

This is actually a long and formatted comment on the last answer. Still not knowing any Python, I am a bit disappointed by not using join to get commas between each address (like suggested by liori). Replacing the comma with some space feels like walking away from the problem, and is not going to learn anyone anything. We don't sacrifice quality here at Stack Overflow! ;-)

I just typed the following on my Mac:

$ python
Python 2.5.1 (r251:54863, Feb  6 2009, 19:02:12) 
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

>>> lst = [ u'one@example.com', u'two@example.com', u'three@example.com' ]
>>> print lst
[u'one@example.com', u'two@example.com', u'three@example.com']

>>> print ', '.join(lst)
one@example.com, two@example.com, three@example.com

>>> print 'Emails: [%s]' % ', '.join(lst)
Emails: [one@example.com, two@example.com, three@example.com]

>>> lst = [ u'one@example.com' ]
>>> print lst
[u'one@example.com']

>>> print ', '.join(lst)
one@example.com

>>> print 'Emails: [%s]' % ', '.join(lst)
Emails: [one@example.com]

>>> s = u'one@example.com'
>>> print s
one@example.com

>>> print ', '.join(s)
o, n, e, @, e, x, a, m, p, l, e, ., c, o, m

Makes perfect sense to me... Now, using a different separator for the last item (like one@example.com, two@example.com and three@example.com) will need some more work, but printing the very same separator between each item should not be complicated at all.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top