你可以如何从文本文件中提取数字,同时指出它们出现在哪一行?(Python)

huangapple go评论99阅读模式
英文:

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

问题

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 5 on line 2
I found 7 on line 2
I found 6 on line 2
I found 6 on line 2
I found 6 on line 2
I found 4 on line 2
I found 3 on line 2
I found 7 on line 2
I found 8 on line 2
I found 3 on line 2
I found 4 on line 2
I found 9 on line 2

英文:

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).

答案1

得分: 1

你写了
for line in f:

如果你使用
[enumerate](https://docs.python.org/3/library/functions.html#enumerate)
来包装文件迭代器,你的问题就变得简单了。
for line_num, line in enumerate(f):
...
                print('我在第', letter, '行找到了', line_num)

<details>
<summary>英文:</summary>

You wrote
for line in f:

If you wrap that file iterator with
[enumerate](https://docs.python.org/3/library/functions.html#enumerate)
your problem becomes simple.
for line_num, line in enumerate(f):
...
                print(&#39;I found &#39;, letter, &#39;on line number&#39;, line_num)

</details>



# 答案2
**得分**: 0

你可以使用groupby函数(来自itertools模块)与str.isdigit函数来分离字符序列中交替出现的数字和非数字字符。

要获取行号,可以在for循环中使用enumerate():

```python
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("在第", lineNumber, "行找到", number)

输出结果:

在第 1 行找到 55
在第 1 行找到 6
在第 1 行找到 6
在第 1 行找到 6
在第 1 行找到 6
在第 1 行找到 9
在第 1 行找到 9
在第 2 行找到 57
在第 2 行找到 6
在第 2 行找到 66
在第 2 行找到 4
在第 2 行找到 3
在第 2 行找到 78
在第 3 行找到 3
在第 3 行找到 4
在第 3 行找到 978
在第 3 行找到 67654
在第 4 行找到 4
在第 4 行找到 7
在第 4 行找到 6
在第 4 行找到 5

希望这对你有帮助。

英文:

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 = (&quot;&quot;.join(digits) for isNum,digits 
               in groupby(line,str.isdigit) if isNum)
    for number in numbers:
        print(&quot;I found &quot;,number,&quot;on line&quot;,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

huangapple
  • 本文由 发表于 2023年3月7日 02:19:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75654443.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定