Parsing info into a 2d list

Alex Martelli aleaxit at yahoo.com
Thu Oct 21 00:17:56 CEST 2004


Matthew Walsh <matt.walsh at gmail.com> wrote:

> Whats wrong with the following code?
> using pythonWin I get the following error:
> 
> Traceback (most recent call last):
>   File
> "C:\Python23\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py",
> line 310, in RunScript
>     exec codeObject in __main__.__dict__
>   File "C:\Documents and Settings\Administrator\My
> Documents\file_parser.py", line 16, in ?
>     rownum = range(eval(nrows))
> TypeError: object doesn't support item assignment
> 
> >>CODE
> f = open('f:\input.dat')
> feed= f.read()
> lines = feed.split('\n')
> ncols = lines[0].split()[1]
> nrows = lines[1].split()[1]
> xllcorner = lines[2].split()[1]
> yllcorner = lines[3].split()[1]
> cellsize = lines[4].split()[1]
> NoData = lines[5].split()[1]
> 
> colnum = range(eval(ncols))
> rownum = range(eval(nrows))
> 
> for r in rownum:
>     for c in colnum:
>         slope[r][c]= eval(lines[6+r].split()[c])
>         
> f.close()
> >>END OF CODE

Looks like the message is off by a few lines.  The item assignment
you're trying to do is in the body of the nested for, and since 'slope'
isn't ever assigned anything anywhere it's hard to guess what it is, but
apparently its items don't support item assignment.


Alex



More information about the Python-list mailing list