[Tutor] manipulating data

Kent Johnson kent37 at tds.net
Thu Nov 15 17:59:45 CET 2007


Bryan Fodness wrote:
> I try this,
> 
> f = open('TEST1.MLC')
> 
> fields = {}
> 
> for line in f:
>     if line.split()[0] == 'Field':
>         field = int(line.split()[-1])
>     elif line.split()[0] == 'Leaf':
>         fields[field] = line.split()[-1]
>     else:
>         line = f.next()
> 
> and get,
> 
> Traceback (most recent call last):
>   File "<pyshell#1>", line 1, in <module>
>     line.split()[0]
> IndexError: list index out of range

For blank lines, line.split() is [] so there is no line.split()[0]. Try 
skipping blank lines before you split.

Kent


More information about the Tutor mailing list