NEWBIE: Tokenize command output

bruno at modulix onurb at xiludom.gro
Fri May 12 15:32:31 CEST 2006


Tim Chase wrote:
>>> starLines = [line for line in p.readlines() if line.startswith("*")]
>>
>>
>> files are iterators, so no need to use readlines() (unless it's an old
>> Python version of course):
>>
>> starLines = [line for line in p if line.startswith("*")]
> 
> 
> Having started with some old Python, it's one of those things I "know",
> but my coding fingers haven't yet put into regular practice. :)

I reeducated my fingers after having troubles with huge files !-)

>>> or you may optionally want to prune of the "\n" characters in the
>>> process:
>>>
>>> starLines = [line[:-1] for line in p.readlines() if
>>> line.startswith("*")]
>>
>>
>> *please* use str.rstrip() for this:
>> starLines = [line.rstrip() for line in p if line.startswith("*")]
> 
> 
> They can yield different things, no?
> 
>>>> s = "abc  \n"
>>>> s[:-1]
> 'abc  '
>>>> s.rstrip()
> 'abc'
>>>> s.[:-1] == s.rstrip()
> False
> 
> If trailing space matters, you don't want to throw it away with rstrip().

then use rstrip('\n') - thanks for this correction.

> Otherwise, just to be informed, what advantage does rstrip() have over
> [:-1] (if the two cases are considered uneventfully the same)?

1/ if your line doesn't end with a newline, line[:-1] will still remove
the last caracter.

2/ IIRC, if you don't use universal newline and the file uses the
DOS/Windows newline convention, line[:-1] will not remove the CR - only
the LF (please someone correct me if I'm wrong here).

I know this may not be a real issue in the actual case, but using
rstrip() is still a safer way to go IMHO - think about using this same
code to iterate over a list of strings without newlines...

-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"



More information about the Python-list mailing list