views:

235

answers:

8

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?

+4  A: 

In Python:

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

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

liori
I'd say: trying this in HTML or JavaScript is simply not recommended, unless the string that represents the whole list is documented to actually look like that in all versions of Python...?
Arjan
Actually, using this string format idiom leaves you with a long *unicode* string: `u'[email protected],[email protected],[email protected]'`. - Not exaclty what the OP wanted.
ThomasH
@ThomasH, so is that concatenated string in your comment above also **printed** including the `u'..'` "debug" info? I don't know any Python, but as in your own answer `str()` is said to be invoked implicitly, then why not in liori's answer?
Arjan
Well, I guess the OP implicitly calls repr. Then... well, the OP probably should change that.
liori
you can just use the join filter in a django template... actually I'll just add it as an answer ;)
Jiaaro
@Arjan Actually, I was probably mislead by the Python interactive shell. The **types** of *str(u'[email protected]')* (<type 'str'>) and *'%s' % u'[email protected]'* (type 'unicode') are different, and the shell shows this. But if you actually **print** those two expression you get the same output. And your argument is right, since the format string '%s' asks for the str() representation of the argument, which speak for the implicit application of str(). Don't know why this is not the case.
ThomasH
+1  A: 

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')
ThomasH
+2  A: 

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

Arjan
*"u'...'" * is a print representation of a Python unicode string. It simply inidcates that the underlying strings are unicode strings (which is good), and that you have to take care once you serialize them (to a file, to the screen, to a byte-string representation, etc.). I aggree with you, the list is not the important thing here, it's the individual strings which should be taken care of, as I outlined in my <a href="http://stackoverflow.com/questions/1222508/dropping-the-unicode-markers/1222595#1222595">answer</a>.
ThomasH
A: 

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.

JAB
+2  A: 
"[{0}]".format(", ".join(python_email_list))

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

gavoja
A: 

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.

anteatersa
That would surely give you one excessive comma in the output?
Arjan
Thank you. This client side solution works with the following modifications.{% for email in Emails %}{{ email|escape}}{% endfor %}
In the question he says he would like a comma between the email addresses.
anteatersa
Dear anteatresa, The crux of the problem was to drop the unicode indicator and if possible solving it on the html itself. Adding a comma in between was a nice-to-have. I can work a little more on that and achieve the same. In the above stated improved solution on your suggestion is using   so that the simple for-loop will not give me the comma after the last list item. Thanks once more !
@anteatersa, but wouldn't your solution *also* print that comma *after* the last address? @Sharma Anil, I feel using `join` to get commas *between* the email addresses (but not after the last one) really was an easier solution. (And in my opinion anything is much better than using ` ` -- just consider what will happen if the number of addresses is too long to fit on a single line in the browser.)
Arjan
@Arjan, please read the added comment on Jim Robert's suggestion to use join:", " - regards.
A: 

In the template

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

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

Jiaaro
A: 

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.

Arjan