Python 使用正则表达式从文本文件中提取的字符串的总和

Python 使用正则表达式从文本文件中提取的字符串的总和,python,regex,sum,Python,Regex,Sum,我正在学习python,我的课堂作业需要一些帮助 我有一个文件,里面有文字和数字。有些行有一到三个数字,有些行根本没有数字 我需要: 使用正则表达式仅从文件中提取数字 求所有数字的和 我用正则表达式提取了所有的数字。我试图得到所有数字的总和,但我只是得到每行有数字的总和。我一直在用不同的方法来完成这项任务,这是我最接近正确的方法 我知道我遗漏了一些关键部分,但我不确定我做错了什么 这是我的密码: import re text = open('text_numbers.txt') for lin

我正在学习python,我的课堂作业需要一些帮助

我有一个文件,里面有文字和数字。有些行有一到三个数字,有些行根本没有数字

我需要:

  • 使用正则表达式仅从文件中提取数字

  • 求所有数字的和

  • 我用正则表达式提取了所有的数字。我试图得到所有数字的总和,但我只是得到每行有数字的总和。我一直在用不同的方法来完成这项任务,这是我最接近正确的方法

    我知道我遗漏了一些关键部分,但我不确定我做错了什么

    这是我的密码:

    import re
    text = open('text_numbers.txt')
    
    for line in text:
        line = line.strip()
        y = re.findall('([0-9]+)',line)
    
        if len(y) > 0:
            print sum(map(int, y))
    
    f = open('regex_sum_text.txt', 'r').read().strip()
    y = re.findall('[0-9]+', f)
    l = [int(s) for s in y]
    s = sum(l)
    print(s)
    
    我得到的结果是这样的 (每个都是一行的总和):

    14151

    8107

    16997

    18305

    3866

    它必须是这样的一个和(所有数字的和):

    134058

    这就是你要找的吗

    import re
    text = open('text_numbers.txt')
    data=text.read()
    print sum(map(int,re.findall(r"\b\d+\b",data)))
    

    使用
    .read
    字符串
    格式获取内容

    我不太懂python,但我可以给出一个简单的解决方案。
    import re
    sample = open ('text_numbers.txt')
    total =0
    dignum = 0 
    
    for line in sample:
        line = line.rstrip()
        dig= re.findall('[0-9]+', line)
    
        if len(dig) >0:
            dignum += len(dig)
            linetotal= sum(map(int, dig))
            total += linetotal
    
    print 'The number of digits are:  ' 
    print dignum
    print 'The sum is: '
    print total     
    print 'The sum ends with: '
    print  total % 1000
    
    试试这个


    你可以把它压缩成一行,但这只是为了好玩

    我第一次尝试使用正则表达式来回答问题,我发现阅读别人的代码是一种很好的练习技巧

    import re # import regular expressions
    chuck_text = open("regex_sum_286723.txt")
    numbers = []
    Total = 0
    for line in chuck_text:
        nmbrs = re.findall('[0-9]+', line)
        numbers = numbers + nmbrs 
    for n in numbers:
        Total = Total + float(n)
    print "Total = ", Total 
    
    thanx和Beer在“理解列表”中只列出了一行,虽然他的“r”似乎不需要,但不确定它的作用。但它读起来很漂亮,我读了两个像我的答案一样的循环,感到更加困惑

    import re
    print sum([int(i) for i in re.findall('[0-9]+',open("regex_sum_286723.txt").read())])
    

    这是我解决这个问题的办法

    import re
    
    file = open('text_numbers.txt')
    sum = 0 
    
    for line in file:
        line = line.rstrip()
        line = re.findall('([0-9]+)', line)
        for i in line:
            i = int(i)
            sum += i    
    
    print(sum)
    
    第一个for循环中的行元素也是列表,我使用第二个for循环将其元素从字符串转换为整数,以便对它们求和。

    以下是我的代码:

    import re
    text = open('text_numbers.txt')
    
    for line in text:
        line = line.strip()
        y = re.findall('([0-9]+)',line)
    
        if len(y) > 0:
            print sum(map(int, y))
    
    import re
    
    fl=open('regex_sum_7469.txt')
    ls=[]
    
    for x in fl: #create a list in the list
       x=x.rstrip()
       print x
       t= re.findall('[0-9]+',x) #all numbers
       for d in t: #for loop as there a empthy values in the list a
            ls.append(int(d))
    print (sum(ls))
    
    f = open('regex_sum_text.txt', 'r').read().strip()
    y = re.findall('[0-9]+', f)
    l = [int(s) for s in y]
    s = sum(l)
    print(s)
    
    另一个较短的方法是:

    with open('regex_sum_text.txt', 'r') as f:
        total = sum(map(int, re.findall(r'[0-9]+', f.read())))
    
    print(total)
    

    将打印和(map(int,y))更改为x+=sum(map(int,y)),并在for循环之前的顶部添加x=0。在for循环的末尾,打印Xtanks Ryan。在我的一个变体中,我有一些类似的东西,但无法让它工作。我不得不修改“导入np”以使其工作。希望它有帮助:)嗨,安德鲁,欢迎使用SO。这里不鼓励只使用代码的答案,因为它们不会教别人如何编写代码。你能编辑你的帖子来解释你的代码示例是做什么的,以及它是如何回答这个问题的吗?谢谢。虽然这段代码可以回答这个问题,但提供关于为什么和/或如何回答这个问题的额外上下文可以提高其长期价值。
    with open('regex_sum_text.txt', 'r') as f:
        total = sum(map(int, re.findall(r'[0-9]+', f.read())))
    
    print(total)
    
    import re
    print(sum(int(value) for value in re.findall('[0-9]+', open('regex_sum_1128122.txt').read())))