Search code examples
pythontextextract

How do you extract the numbers from a text file while also stating the line each appears on? (Python)


I need to know how to extract a number from a text file while also having it tell me what line the number is on. But i need it to paste the output to a new text file

Program:

fname = input("Enter file name: ")
with open(fname, 'r') as f:
    for line in f:
        words = line.split()
        for i in words:
            for letter in i:
                if(letter.isdigit()):
                    print('I found ',letter)

Text File:

fufge55gf6eh6h6eh6f9hg9hj
vgcg57vgr6vgn66cg4w3vgv78v
gv3c4ghc978c67654cchch
v4bci7r6ec5wgv

Output:

Enter file name: superpleasework.txt
I found  5
I found  5
I found  6
I found  6
I found  6
I found  6
I found  9
I found  9
I found  5
I found  7
I found  6
I found  6
I found  6
I found  4
I found  3
I found  7
I found  8
I found  3
I found  4
I found  9
...etc

I was expecting it to look like this:

I found 55 on line 1
I found 6 on line 1
I found 6 on line 1
I found 6 on line 1
I found 6 on line 1
I found 9 on line 1
I found 9 on line 1...etc

I need it to tell me the line each number is on (without separating the numbers that are together).


Solution

  • You could use the groupby function (form itertools) with the str.isdigit function to separate the lines on the alternating numeric and non-numeric sequences of characters.

    To get the line numbers, use enumerate() in your for-loop:

    from itertools import groupby
    for lineNumber,line in enumerate(f,1):
        numbers = ("".join(digits) for isNum,digits 
                   in groupby(line,str.isdigit) if isNum)
        for number in numbers:
            print("I found ",number,"on line",lineNumber)
    

    output:

    I found  55 on line 1
    I found  6 on line 1
    I found  6 on line 1
    I found  6 on line 1
    I found  6 on line 1
    I found  9 on line 1
    I found  9 on line 1
    I found  57 on line 2
    I found  6 on line 2
    I found  66 on line 2
    I found  4 on line 2
    I found  3 on line 2
    I found  78 on line 2
    I found  3 on line 3
    I found  4 on line 3
    I found  978 on line 3
    I found  67654 on line 3
    I found  4 on line 4
    I found  7 on line 4
    I found  6 on line 4
    I found  5 on line 4