#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    6
    Rep 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.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    139
    Rep Power
    4

    For Loop


    Code:
    Cnt = 0
    
    Values=open('filename','r').readlines()
    
    for Value in Values :
      if int(Value) >= 9.79 : Cnt +=1
    
    print Cnt
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    138
    Rep Power
    2
    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'
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    138
    Rep Power
    2
    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
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    6
    Rep Power
    0
    Thank you. I will try this as soon as my laptop is re-installed.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    6
    Rep 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
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    138
    Rep Power
    2
    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:]:
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    6
    Rep Power
    0

    ...


    >...
    Last edited by superflyish; March 16th, 2013 at 12:30 PM. Reason: Some details were wrong
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    6
    Rep 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.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    138
    Rep Power
    2
    ' 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.
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    6
    Rep Power
    0
    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.

IMN logo majestic logo threadwatch logo seochat tools logo