lines property issue
It seems that lines properties initialize to [''] -- shouldn't they initialize to []? I would have assumed that lines properties are parsed with splitlines(), but ''.splitlines() returns []. '\n'.splitlines(), however, returns ['']. Can someone enlighten me on this? This is a problem, for example, in ZPT b/c I'd like to do things like tal:condition="object/lines_property". Thanks! David Mozilla 1.5/Win Zope 2.6.2/Linux -- David Chandek-Stark dc@duke.edu
On Mon, 2003-11-17 at 14:32, David Chandek-Stark wrote:
I would have assumed that lines properties are parsed with splitlines(), but ''.splitlines() returns []. '\n'.splitlines(), however, returns [''].
Can someone enlighten me on this?
'' is an empty string, whereas '\n' is a string that contains a newline character. The splitlines() method (like other split methods) returns what was found before the split character and (if applicable) what is found after each subsequent split character. No newline is found in '' so your return is an empty list. Searching '\n' a newline *is* found, so your return is populated with what came before newline: an empty string. Since nothing was found after that newline, it's a one-element list. HTH, Dylan
On Mon, 2003-11-17 at 15:17, Dylan Reinhardt wrote:
The splitlines() method (like other split methods) returns what was found before the split character and (if applicable) what is found after each subsequent split character.
Actually, this is not entirely correct, now that I read it. string.splitlines() is a slightly special case in that it doesn't include what is after the last newline character. If you used string.split('\n') you'd get an element for what came after the last newline character. Ex:
foo = 'a\nb\n' foo.splitlines() ['a', 'b'] foo.split('\n') ['a', 'b', '']
FWIW, Dylan
Ah, that must be it! ''.split('\n') indeed returns ['']. May have to check the issue collector on this. Why wouldn't they use splitlines()? Is it a newer method? --David Dylan Reinhardt wrote:
On Mon, 2003-11-17 at 15:17, Dylan Reinhardt wrote:
The splitlines() method (like other split methods) returns what was found before the split character and (if applicable) what is found after each subsequent split character.
Actually, this is not entirely correct, now that I read it.
string.splitlines() is a slightly special case in that it doesn't include what is after the last newline character. If you used string.split('\n') you'd get an element for what came after the last newline character.
Ex:
foo = 'a\nb\n' foo.splitlines()
['a', 'b']
foo.split('\n')
['a', 'b', '']
FWIW,
Dylan
-- David Chandek-Stark dc@duke.edu
I understand how splitlines() works -- just don't understand how Zope gets '\n' from an empty value. E.g., if you submit a form with an empty <textarea> element, the resulting value should be an empty string '', which splitlines() would turn into []. --David Dylan Reinhardt wrote:
On Mon, 2003-11-17 at 14:32, David Chandek-Stark wrote:
I would have assumed that lines properties are parsed with splitlines(), but ''.splitlines() returns []. '\n'.splitlines(), however, returns [''].
Can someone enlighten me on this?
'' is an empty string, whereas '\n' is a string that contains a newline character.
The splitlines() method (like other split methods) returns what was found before the split character and (if applicable) what is found after each subsequent split character.
No newline is found in '' so your return is an empty list.
Searching '\n' a newline *is* found, so your return is populated with what came before newline: an empty string. Since nothing was found after that newline, it's a one-element list.
HTH,
Dylan
-- David Chandek-Stark dc@duke.edu
participants (2)
-
David Chandek-Stark -
Dylan Reinhardt