简体   繁体   English

用于在一行中查找数字的正则表达式 (Python)

[英]Regular Expression for Finding Numbers in a Line (Python)

I'm just learning about regular expressions and I need to read in a text file and find every instance of a number and find the sum of all the numbers.我只是在学习正则表达式,我需要阅读一个文本文件并找到一个数字的每个实例并找到所有数字的总和。

import re

sum = 0
list_of_numbers = list()
working_file = open("sample.txt", 'r')
for line in working_file:
    line = line.rstrip()
    working_list = re.findall('[0-9]+', line)
    if len(working_list) != 1:
        continue
    print(working_list)
    for number in working_list:
        num = int(number)
        list_of_numbers.append(num)
for number in list_of_numbers:
    sum += number
print(sum)

I put the print(working_list) in order to try and debug it and see if all the numbers are getting found correctly and I've seen, by manually scanning the text file, that some numbers are being skipped while others are not.我把print(working_list)为了尝试和调试它,看看是否所有的数字都被正确找到,我已经看到,通过手动扫描文本文件,有些数字被跳过,而另一些则没有。 I'm confused as to why as I thought my regular expression guaranteed that any string with any amount of digits will be added to the list.我很困惑,因为我认为我的正则表达式保证任何具有任意数量数字的字符串都将添加到列表中。

Here is the file .这是文件

你只验证只有一个数字的行,所以有两个数字的行将被跳过,因为if len(working_list) != 1: continue ,这基本上是说“如果这一行没有确切的一个数字,那么跳过”,您的意思可能类似于if len(working_list) < 1: continue

I would do it like:我会这样做:

import re

digits_re = re.compile(r'(\d+(\.\d+)?)') 
with open("sample.txt", 'r') as fh:
  numbers = [float(match[0]) for match in digits_re.findall(fh.read())]
print(sum(numbers))

or like you're doing with ints just或者就像你在用整数做的一样

import re

digits_re = re.compile(r'(\d+)') 
with open("sample.txt", 'r') as fh:
  numbers = [int(match[0]) for match in digits_re.findall(fh.read())]
print(sum(numbers))
h = open('file.txt')
nos = list()
for ln in h:
    fi = re.findall('[0-9]+', ln)
    for i in fi:
        nos.append(int(i))
print('Sum:', sum(nos))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM