help me to find the error
tjreedy at udel.edu
Fri Jul 10 15:30:04 EDT 2009
jhinak sen wrote:
> i am a beginner in python language,
Welcome to Python.
> i am trying with this programme :
> to find the addition and mean from a data set in a file and writing the
> mean and sum in some other file :
This is three things: input data from, perform calculation, output data
to file. I would start simpler. See below.
You did two things right that too many people do not do.
1. You posted a complete chunk of code.
> *#! /usr/bin/env python
> import re
> import cPickle as p
> import math
> from numpy import *
You never use these.
> f0= open("temp9","r+").readlines()
2. You posted the complete traceback (instead of the annoying 'program
didn't work ;-).
> "" *Traceback (most recent call last):
> File "./temporary1.py", line 24, in <module>
> AttributeError: 'list' object has no attribute 'close'*
> please help to to find the error.
Seeing that, any of us could tell you thought f0 was a file but it was
actually a list. Looking back up through the code, people found the
definition -- the output of file.readlines, which is a list.
> or suggest some simpler or better way
Develop more incrementally. If you edit with IDLE, for instance, and hit
RUN (F5), it takes about a second to see the result for a small program
like this. I would have suggested starting with
data = [(1,2), (3,4)]
print "input: ", a, b, "output: ", tot, ave
and fill in ... until the output was correct.
Then change data to ['1 2', '3 4'] and revise until correct.
At that point, change data to open(....) and the program should
otherwise work without change because a list of strings and a file are
both iterators that produce a sequence of strings.
Now, if you want, worry about formating the output, removing the echo of
Very last, send to output to a disk file instead of the screen. For
development, that is a nuisance because it takes time to open the file
to check results. So only do that when you already know the results are
correct. Note that real programs used repeatedly ofter do not hard code
an output file. If run from a command line in a comman window, screen
output can be redirected to a file with '>': "myprog > outfile".
Terry Jan Reedy
More information about the Python-list