Sharma Anil
Sharma Anil

Reputation:

Dropping the Unicode markers in Html output

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

[u'[email protected]',u'[email protected]',u'[email protected]']

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

Emails: [u'[email protected]',u'[email protected]',u'[email protected]']

I would like it to look like this:

Emails: [ [email protected], [email protected], [email protected] ]

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

Upvotes: 1

Views: 401

Answers (8)

Arjan
Arjan

Reputation: 23529

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'[email protected]', u'[email protected]', u'[email protected]' ]
>>> print lst
[u'[email protected]', u'[email protected]', u'[email protected]']

>>> print ', '.join(lst)
[email protected], [email protected], [email protected]

>>> print 'Emails: [%s]' % ', '.join(lst)
Emails: [[email protected], [email protected], [email protected]]

>>> lst = [ u'[email protected]' ]
>>> print lst
[u'[email protected]']

>>> print ', '.join(lst)
[email protected]

>>> print 'Emails: [%s]' % ', '.join(lst)
Emails: [[email protected]]

>>> s = u'[email protected]'
>>> print s
[email protected]

>>> 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 [email protected], [email protected] and [email protected]) will need some more work, but printing the very same separator between each item should not be complicated at all.

Upvotes: 0

Jiaaro
Jiaaro

Reputation: 76918

In the template

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

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

Upvotes: 1

anteatersa
anteatersa

Reputation: 1508

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.

Upvotes: -1

gavoja
gavoja

Reputation: 377

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

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

Upvotes: 2

JAB
JAB

Reputation: 21089

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.

Upvotes: 0

Arjan
Arjan

Reputation: 23529

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).

Upvotes: 2

ThomasH
ThomasH

Reputation: 23516

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'[email protected]'

In [551]: print ss   # uses str() implicitly
Out[552]: [email protected]

In [552]: str(ss)
Out[552]: '[email protected]'

In [553]: repr(ss)
Out[553]: "u'[email protected]'"

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')

Upvotes: 1

liori
liori

Reputation: 42277

In Python:

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

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

Upvotes: 4

Related Questions