<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 12pt;
font-family:Calibri
}
--></style></head>
<body class='hmmessage'><div dir='ltr'>Hi Arijit,<div><br></div><div>I have modified your program a bit and it is working fine for me. Values greater than 5 are being printed. Here is the code:</div><div><br></div><div><div>f = open ("D:\\digi_2.txt", "r+") </div><div>lines = f.readlines() </div><div>number_list = [] </div><div>for line in lines:     </div><div><span class="Apple-tab-span" style="white-space:pre">   </span>print line</div><div><span class="Apple-tab-span" style="white-space:pre">   </span>for number in line.split(','): </div><div><span class="Apple-tab-span" style="white-space:pre">         </span>#number_list.append(float(number)) </div><div><span class="Apple-tab-span" style="white-space:pre">             </span>number_list.append(int(number)) </div><div><br></div><div>s_data = [] </div><div>for i in number_list:</div><div><span class="Apple-tab-span" style="white-space:pre">   </span>print i</div><div><span class="Apple-tab-span" style="white-space:pre">      </span>#if number_list[i] > 5: </div><div><span class="Apple-tab-span" style="white-space:pre">     </span>if(i>5):</div><div><span class="Apple-tab-span" style="white-space:pre">          </span>s_data.append(i)</div><div><br></div><div>for x in s_data:</div><div><span class="Apple-tab-span" style="white-space:pre">     </span>print 'Data val:', float(x) </div><div><br></div><div><br></div><div><br></div><div>Regards</div><div>Suhas</div><br><div><div id="SkyDrivePlaceholder"></div><hr id="stopSpelling">To: tutor@python.org<br>From: arijit.ukil@tcs.com<br>Date: Fri, 22 Mar 2013 12:54:01 +0530<br>Subject: [Tutor] Please Help<br><br><font size="2" face="sans-serif">Hi,</font>
<br>
<br><font size="2" face="sans-serif">I have another small problem. Pls help.</font>
<br>
<br><font size="2" face="sans-serif">I have written the following code:</font>
<br>
<br><font size="2" face="sans-serif">f = open ("digi_2.txt", "r+")</font>
<br><font size="2" face="sans-serif">lines = f.readlines()</font>
<br><font size="2" face="sans-serif">for line in lines:</font>
<br><font size="2" face="sans-serif">    number_list = []</font>
<br><font size="2" face="sans-serif">    for number in line.split(','):</font>
<br><font size="2" face="sans-serif">        number_list.append(float(number))</font>
<br>
<br><font size="2" face="sans-serif">s_data = []</font>
<br><font size="2" face="sans-serif">for i in range(len(number_list)):</font>
<br><font size="2" face="sans-serif">    if number_list[i] >
5:</font>
<br><font size="2" face="sans-serif">        s_data =
number_list[i]</font>
<br>
<br><font size="2" face="sans-serif">print 'Data val:', s_data</font>
<br>
<br>
<br><font size="2" face="sans-serif"><b>The problem is: it is printing only
the last value, not all the values. In this case '10', </b></font><font size="2" color="blue" face="sans-serif"><b>not</b></font><font size="2" face="sans-serif"><b>
'9,8,6,10'.</b></font>
<br>
<br>
<br>
<br><font size="2" face="sans-serif">Regards,<br>
Arijit Ukil<br>
Tata Consultancy Services<br>
Mailto: arijit.ukil@tcs.com<br>
Website: </font><a href="http://www.tcs.com/" target="_blank"><font size="2" face="sans-serif">http://www.tcs.com</font></a><font size="2" face="sans-serif"><br>
____________________________________________<br>
Experience certainty.        IT Services<br>
                
       Business Solutions<br>
                
       Outsourcing<br>
____________________________________________</font>
<br>
<br>
<br>
<table width="100%">
<tbody><tr valign="top">
<td><font size="1" color="#5f5f5f" face="sans-serif">From:</font>
</td><td><font size="1" face="sans-serif">Amit Saha <amitsaha.in@gmail.com></font>
</td></tr><tr valign="top">
<td><font size="1" color="#5f5f5f" face="sans-serif">To:</font>
</td><td><font size="1" face="sans-serif">Arijit Ukil <arijit.ukil@tcs.com></font>
</td></tr><tr>
<td valign="top"><font size="1" color="#5f5f5f" face="sans-serif">Cc:</font>
</td><td><font size="1" face="sans-serif">tutor@python.org</font>
</td></tr><tr valign="top">
<td><font size="1" color="#5f5f5f" face="sans-serif">Date:</font>
</td><td><font size="1" face="sans-serif">03/21/2013 05:30 PM</font>
</td></tr><tr valign="top">
<td><font size="1" color="#5f5f5f" face="sans-serif">Subject:</font>
</td><td><font size="1" face="sans-serif">Re: [Tutor] Please Help</font></td></tr></tbody></table>
<br>
<hr noshade="">
<br>
<br>
<br><tt><font size="2">Hi Arijit,<br>
<br>
On Thu, Mar 21, 2013 at 8:42 PM, Arijit Ukil <arijit.ukil@tcs.com>
wrote:<br>
><br>
> I am new to python. I like to calculate average of the numbers by
reading<br>
> the file 'digi_2.txt'. I have written the following code:<br>
><br>
> def average(s): return sum(s) * 1.0 / len(s)<br>
><br>
> f = open ("digi_2.txt", "r+")<br>
><br>
> list_of_lists1 = f.readlines()<br>
><br>
><br>
> for index in range(len(list_of_lists1)):<br>
><br>
><br>
>     tt = list_of_lists1[index]<br>
><br>
>     print 'Current value :', tt<br>
><br>
> avg =average (tt)<br>
><br>
><br>
> This gives an error:<br>
><br>
> def average(s): return sum(s) * 1.0 / len(s)<br>
> TypeError: unsupported operand type(s) for +: 'int' and 'str'<br>
><br>
> I also attach the file i am reading.<br>
><br>
><br>
><br>
> Please help to rectify.<br>
<br>
The main issue here is that when you are reading from a file, to<br>
Python, its all strings. And although, 'abc' + 'def' is valid, 'abc' +<br>
5 isn't (for example). Hence, besides the fact that your average<br>
calculation is not right, you will have to 'convert' the string to an<br>
integer/float to do any arithmetic operation on them. (If you know C,<br>
this is similar to typecasting). So, coming back to your program, I<br>
will first demonstrate you a few things and then you can write the<br>
program yourself.<br>
<br>
If you were to break down this program into simple steps, they would be:<br>
<br>
1. Read the lines from a file (Assume a generic case, where you have<br>
more than one line in the file, and you have to calculate the average<br>
for each such row)<br>
2. Create a list of floating point numbers for each of those lines<br>
3. And call your average function on each of these lists<br>
<br>
You could of course do 2 & 3 together, so you create the list and call<br>
the average function.<br>
<br>
So, here is step 1:<br>
<br>
with open('digi.txt','r') as f:<br>
    lines = f.readlines()<br>
<br>
Please refer to<br>
</font></tt><a href="http://docs.python.org/2/tutorial/inputoutput.html#methods-of-file-objects" target="_blank"><tt><font size="2">http://docs.python.org/2/tutorial/inputoutput.html#methods-of-file-objects</font></tt></a><tt><font size="2"><br>
for an explanation of the advantage of using 'with'.<br>
<br>
Now, you have *all* the lines of the file in 'lines'. Now, you want to<br>
perform step 2 for each line in this file. Here you go:<br>
<br>
for line in lines:<br>
    number_list = []<br>
    for number in line.split(','):<br>
        number_list.append(float(number))<br>
<br>
 (To learn more about Python lists, see<br>
</font></tt><a href="http://effbot.org/zone/python-list.htm" target="_blank"><tt><font size="2">http://effbot.org/zone/python-list.htm</font></tt></a><tt><font size="2">).
It is certainly possible to<br>
use the index of an element to access elements from a list, but this<br>
is more Pythonic way of doing it. To understand this better, in the<br>
variable 'line', you will have a list of numbers on a single line. For<br>
example: 1350696461, 448.0, 538660.0, 1350696466, 448.0. Note how they<br>
are separated by a ',' ? To get each element, we use the split( )<br>
function, which returns a list of the individual numbers. (See:<br>
</font></tt><a href="http://docs.python.org/2/library/stdtypes.html#str.split" target="_blank"><tt><font size="2">http://docs.python.org/2/library/stdtypes.html#str.split</font></tt></a><tt><font size="2">).
And then,<br>
we use the .append() method to create the list. Now, you have a<br>
number_list which is a list of floating point numbers for each line.<br>
<br>
Now, step 2 & 3 combined:<br>
<br>
for line in lines:<br>
    number_list = []<br>
    for number in line.split(','):<br>
        number_list.append(float(number))<br>
    print average(number_list)<br>
<br>
Where average( ) is defined as:<br>
<br>
def average(num_list):<br>
    return sum(num_list)/len(num_list)<br>
<br>
<br>
<br>
There may be a number of unknown things I may have talked about, but i<br>
hope the links will help you learn more and write your program now.<br>
<br>
Good Luck.<br>
-Amit.<br>
<br>
<br>
--<br>
</font></tt><a href="http://amitsaha.github.com/" target="_blank"><tt><font size="2">http://amitsaha.github.com/</font></tt></a><tt><font size="2"><br>
</font></tt>
<br>
<br>=====-----=====-----=====<br>
Notice: The information contained in this e-mail<br>
message and/or attachments to it may contain <br>
confidential or privileged information. If you are <br>
not the intended recipient, any dissemination, use, <br>
review, distribution, printing or copying of the <br>
information contained in this e-mail message <br>
and/or attachments to it are strictly prohibited. If <br>
you have received this communication in error, <br>
please notify us by reply e-mail or telephone and <br>
immediately and permanently delete the message <br>
and any attachments. Thank you<BR>

<BR><br>_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor</div></div>                                     </div></body>
</html>