Newby Question for reading a file

Steve Holden steve at holdenweb.com
Fri Feb 20 02:52:59 CET 2009


alex23 wrote:
> On Feb 20, 5:38 am, Peter Otten <__pete... at web.de> wrote:
>> Yes you can get portions of the line by slicing:
>>
>> for line in open("infile"):
>>     if line[8:18] != line[18:28]:
>>             print line,
> 
> You can also name the slices, which makes the code more clear (IMO):
> 
> endda = slice(8,18)
> begda = slice(18,28)
> for line in open("infile"):
>     if line[endda] != line[begda]:
>             print line,
> 
>> Or you can use the struct module:
> 
> But this is the approach I'd take.

This is one of the lesser-appreciated aspects of Python. I anticipate a
flood of "Named Slices" blog posts round about now. Wonder if I'll be
the first?

regards
 Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC              http://www.holdenweb.com/




More information about the Python-list mailing list