Python Forum
Counting numbers in other lines
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Counting numbers in other lines
#1
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 :)
Reply
#2
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)
Larz60+ write Dec-11-2020, 01:22 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

Fixed for you this time. Please use bbcode tags on future posts.
Reply
#3
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.
LewAlbert likes this post
Reply
#4
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?
Reply
#5
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.
LewAlbert likes this post
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#6
(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.
Reply
#7
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.
LewAlbert likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Random Generator: From Word to Numbers, from Numbers to n possibles Words Yamiyozx 2 1,428 Jan-02-2023, 05:08 PM
Last Post: deanhystad
  Convert list of numbers to string of numbers kam_uk 5 3,022 Nov-21-2020, 03:10 PM
Last Post: deanhystad
  Regular Expressions in Files (find all phone numbers and credit card numbers) Amirsalar 2 4,115 Dec-05-2017, 09:48 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020