Python Forum
Counting numbers in other lines - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Counting numbers in other lines (/thread-31442.html)



Counting numbers in other lines - LewAlbert - Dec-11-2020

Hi, I need to count all numbers from text file using startswith. And I don't know how to count them up after displaying the records I'm interested in because everyone is in a different output line. Any ideas? Thanks in advance :)


RE: Counting numbers in other lines - LewAlbert - Dec-11-2020

Output:
Enter file name: mbox-short X-DSPAM-Confidence: 0.8475 X-DSPAM-Confidence: 0.6178 X-DSPAM-Confidence: 0.6961 X-DSPAM-Confidence: 0.7565 X-DSPAM-Confidence: 0.7626 X-DSPAM-Confidence: 0.7556 X-DSPAM-Confidence: 0.7002 X-DSPAM-Confidence: 0.7615 X-DSPAM-Confidence: 0.7601 X-DSPAM-Confidence: 0.7605 X-DSPAM-Confidence: 0.6959 X-DSPAM-Confidence: 0.7606 X-DSPAM-Confidence: 0.7559 X-DSPAM-Confidence: 0.7605 X-DSPAM-Confidence: 0.6932 X-DSPAM-Confidence: 0.7558 X-DSPAM-Confidence: 0.6526 X-DSPAM-Confidence: 0.6948 X-DSPAM-Confidence: 0.6528 X-DSPAM-Confidence: 0.7002 X-DSPAM-Confidence: 0.7554 X-DSPAM-Confidence: 0.6956 X-DSPAM-Confidence: 0.6959 X-DSPAM-Confidence: 0.7556 X-DSPAM-Confidence: 0.9846 X-DSPAM-Confidence: 0.8509 X-DSPAM-Confidence: 0.9907 Press any key to continue . . .
It look like that and i want to count numbers after ":"

AND MY CODE!
x = input('Enter file name: ')
pos = x.find('.')
plc = 0
if pos == -1:
    xnam = x +'.txt'
    fil = open(xnam)
    for x in fil:
        plc = plc + 1
        x = x.rstrip()
        y = x.startswith('X-DSPAM-Confidence:')
        if y == True:
            dwu = x.find(':')
            cunt = x[dwu+1:].strip()
            print(cunt)



RE: Counting numbers in other lines - MK_CodingSpace - Dec-11-2020

There might be other ways to do it, but you can always use splitlines() command to split a txt file to lines and split() function to split a line to words/numbers etc.


RE: Counting numbers in other lines - deanhystad - Dec-11-2020

Why are you using find(':')? You know where ':' is. You have the number string, all you have to do is convert to a number and add them together. Is it the adding together that you are having a problem with?

I am going to read off a list of numbers and I need you to tell me the total. You do not have a pencil and paper to write the numbers down. How would you do that?


RE: Counting numbers in other lines - perfringo - Dec-11-2020

This is homework so it's about learning.

There is requirement to use str.startswith(). In order to get help fire up interactive interpreter and:

>>> help(str.startswith)
Help on method_descriptor:

startswith(...)
    S.startswith(prefix[, start[, end]]) -> bool

    Return True if S starts with the specified prefix, False otherwise.
    With optional start, test S beginning at that position.
    With optional end, stop comparing S at that position.
    prefix can also be a tuple of strings to try.
Now you know what startswith does. Pending on other requirements you have not disclosed there are several avenues to proceed. You can start at specific index (if content before : is always the same); you can split, strip and provide tuple of numbers to check whether reminder starts with number etc, etc.


RE: Counting numbers in other lines - LewAlbert - Dec-12-2020

(Dec-11-2020, 02:54 PM)deanhystad Wrote: Why are you using find(':')? You know where ':' is. You have the number string, all you have to do is convert to a number and add them together. Is it the adding together that you are having a problem with?

I am going to read off a list of numbers and I need you to tell me the total. You do not have a pencil and paper to write the numbers down. How would you do that?
at the beginning of the program gives the "user" the ability to type in the name of the file that wants to read the search for the dot is to open this file without any extension errors.
The whole text file contains a lot of lines and I didn't know how to add numbers in different places of this file hence my question.


RE: Counting numbers in other lines - deanhystad - Dec-12-2020

Stop thinking about how to write the program and think about ways to solve the problem. If I read off some numbers and you had to add them up, how would you do it? Maybe you would write them all down and add them up. What would you do if you didn't have pencil and paper? How would you add them up then?

After you have multiple solutions to the problem you can start thinking about how to translate those answers into Python code. If you have multiple solutions to start with you will end up with a better program. Some solutions will be easy to translate into clean Python code and some will require convoluted Python code.