The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Help with code please
Discuss Help with code please in the Python Programming forum on Dev Shed. Help with code please Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

March 15th, 2013, 06:26 AM
|
|
Registered User
|
|
Join Date: Mar 2013
Posts: 6
Time spent in forums: 1 h 42 m 52 sec
Reputation Power: 0
|
|
|
Help with code please
I have tried researching for this and I cannot find anything. If someone can help much appreciated.
5.21
4.63
5.24
3.62
1.98
16.47
1.31
1.85
4.26
0.98
1.84
0.51
15.58
2.64
4.32
0.59
0.21
5.41
0.08
4.34
Count the number of values in field [amount] more than or equal to ( 9.79)
If you can tell me the code to get the answer.
Thank you.
|

March 15th, 2013, 09:10 AM
|
|
Contributing User
|
|
Join Date: Aug 2011
Posts: 139
Time spent in forums: 2 Days 3 h 53 m 35 sec
Reputation Power: 2
|
|
|
For Loop
Code:
Cnt = 0
Values=open('filename','r').readlines()
for Value in Values :
if int(Value) >= 9.79 : Cnt +=1
print Cnt
|

March 15th, 2013, 09:44 AM
|
|
Contributing User
|
|
Join Date: Feb 2013
Posts: 138
Time spent in forums: 1 Day 11 h 39 m 41 sec
Reputation Power: 1
|
|
Quote: | Originally Posted by WynnDeezl
Code:
Cnt = 0
Values=open('filename','r').readlines()
for Value in Values :
if int(Value) >= 9.79 : Cnt +=1
print Cnt
|
Have you tried your own program?
Code:
Traceback (most recent call last):
File "count.py", line 6, in <module>
if int(Value) >= 9.79 : Cnt +=1
ValueError: invalid literal for int() with base 10: '5.21\n'
|

March 15th, 2013, 09:48 AM
|
|
Contributing User
|
|
Join Date: Feb 2013
Posts: 138
Time spent in forums: 1 Day 11 h 39 m 41 sec
Reputation Power: 1
|
|
Quote: | Originally Posted by superflyish Count the number of values in field [amount] more than or equal to ( 9.79)
|
Based on WynnDeezl's example above:
Code:
#!/usr/bin/python
count = 0
with open('count', 'r') as f:
values = f.readlines()
for value in values:
if float(value) >= 9.79:
count += 1
print count
|

March 15th, 2013, 03:28 PM
|
|
Registered User
|
|
Join Date: Mar 2013
Posts: 6
Time spent in forums: 1 h 42 m 52 sec
Reputation Power: 0
|
|
|
Thank you. I will try this as soon as my laptop is re-installed.
|

March 15th, 2013, 03:41 PM
|
|
Registered User
|
|
Join Date: Mar 2013
Posts: 6
Time spent in forums: 1 h 42 m 52 sec
Reputation Power: 0
|
|
|
The code keeps coming with an error. I tried fixing the errors but they still don't work. I'll try explain the question again. I have got the code to open the file and read it.
The data is in an excel file.
amount (A1)
5.21 (A2)
4.63 (A3)
5.24 (A4)
3.62 (A5)
1.98 (A6)
16.47 (A7)
1.31 (A8)
1.85 (A9)
4.26 (A10)
0.98 (A11)
1.84 (A12)
0.51 (A13)
15.58 (A14)
2.64 (A15)
4.32 (A16)
0.59 (A17)
0.21 (A18)
5.41 (A19)
0.08 (A20)
4.34 (A21)
The question is:
Count the number of values in field [amount] more than or equal to ( 9.79)
Much appreciated for the help.
Thank you
|

March 16th, 2013, 10:50 AM
|
|
Contributing User
|
|
Join Date: Feb 2013
Posts: 138
Time spent in forums: 1 Day 11 h 39 m 41 sec
Reputation Power: 1
|
|
Quote: | Originally Posted by superflyish The code keeps coming with an error. I tried fixing the errors but they still don't work. I'll try explain the question again. I have got the code to open the file and read it.
The data is in an excel file.
amount (A1)
5.21 (A2)
[...]
The question is:
Count the number of values in field [amount] more than or equal to ( 9.79)
Much appreciated for the help.
Thank you |
What error are you getting? What python version are you using?
If you have to interact with excel, I suggest you look at http://www.python-excel.org/ . If you can save the file as csv file, my code above should work.
Although if you have a heading in the file, change:
Code:
for value in values:
...to:
Code:
for value in values[1:]:
|

March 16th, 2013, 11:27 AM
|
|
Registered User
|
|
Join Date: Mar 2013
Posts: 6
Time spent in forums: 1 h 42 m 52 sec
Reputation Power: 0
|
|
|
...
>...
Last edited by superflyish : March 16th, 2013 at 11:30 AM.
Reason: Some details were wrong
|

March 16th, 2013, 11:29 AM
|
|
Registered User
|
|
Join Date: Mar 2013
Posts: 6
Time spent in forums: 1 h 42 m 52 sec
Reputation Power: 0
|
|
|
I am using 3.2.3 I tried using your code but it kept coming with an error: ValueError: could not convert string to float: ' 5.21,I[N8U7]:75,184,Cold,2254,Cherry\n'
I changed the code to
import csv
with open('random.csv', 'r', newline='') as f:
reader = csv.reader(f)
next(reader)
count = 0
for row in reader:
amount = float(row[0])
if amount >= 9.79:
count += 1
print (count)
That code worked.
|

March 17th, 2013, 07:10 AM
|
|
Contributing User
|
|
Join Date: Feb 2013
Posts: 138
Time spent in forums: 1 Day 11 h 39 m 41 sec
Reputation Power: 1
|
|
Quote:
' 5.21,I[N8U7]:75,184,Cold,2254,Cherry\n' |
Ah.
If you had specified that the file contained more values than just the first one, it would've been much easier to help you.
|

March 17th, 2013, 07:57 AM
|
|
Registered User
|
|
Join Date: Mar 2013
Posts: 6
Time spent in forums: 1 h 42 m 52 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by partoj Ah.
If you had specified that the file contained more values than just the first one, it would've been much easier to help you. |
Sorry about that.
But thanks for the help.
Appreciate it.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|