[Tutor] Please Help

Dave Angel davea at davea.name
Thu Mar 21 12:54:50 CET 2013


On 03/21/2013 06:42 AM, Arijit Ukil wrote:
> I am new to python.

Since you're new to Python, I won't try to supply you an answer using 
list comprehensions, since you've probably not learned them yet.


> I like to calculate average of the numbers by reading
> the file 'digi_2.txt'. I have written the following code:
>
> def average(s): return sum(s) * 1.0 / len(s)

This function presumably expects to be passed a list (or iterable) of 
ints or a list of floats as its argument.  It'll fail if given a list of 
strings.  A comment or docstring to that effect would be useful to 
remind yourself.

>
> f = open ("digi_2.txt", "r+")

Why is there a "plus sign" in the mode string?  Not necessary, since 
you're just going to read the file straight through.

>
> list_of_lists1 = f.readlines()
>

Not a good name, since that's not what readlines() returns.  It'll 
return a list of strings, each string representing one line of the file.

>
> for index in range(len(list_of_lists1)):

Since your file is only one line long, this loop doesn't do much.

>
>
>      tt = list_of_lists1[index]
>
>      print 'Current value :', tt
>

At this point, It is the string read from the last line of the file. 
The other lines are not represented in any way.

> avg =average (tt)
>
>
> This gives an error:
>
> def average(s): return sum(s) * 1.0 / len(s)
> TypeError: unsupported operand type(s) for +: 'int' and 'str'
>
> I also attach the file i am reading.
>
You shouldn't assume everyone can read the "attached" data.  Since it's 
short, you should just include it in your message.  It's MUCH shorter 
than all the irrelevant data you included at the end of your message.

For those others who may be reading this portion of the hijacked thread, 
here's the one line in digi_2.txt

1350696461, 448.0, 538660.0, 1350696466, 448.0


Now to try to solve the problem.  First, you don't specify what the 
numbers in the file will look like.  Looking at your code, I naturally 
assumed you had one value per line.  Instead I see a single line with 
multiple numbers separated by commas.

I'll assume that the data will always be in a single line, or that if 
there are multiple lines, every line but the last will end with a comma, 
and that the last one will NOT have a trailing comma.  If I don't assume 
something, the problem can't be solved.


Since we don't care about newlines, we can read the whole file into one 
string, with the read() function.


f = open ("digi_2.txt", "r")
filedata = f.read()
f.close()

Now we have to separate the data by the commas.

numstsrings = filedata.split(",")

And now we have to convert each of these "numstring" values from a 
string into a float.

nums = []
for numstring in numstrings:
     nums.append(float(numstring))

Now we can call the average function, since we have a list of floats.

avg = average(nums)

Completely untested, so there may be typos in it.





-- 
DaveA


More information about the Tutor mailing list