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

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

本文介绍了使用正则表达式从文本文件中提取的字符串总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚学习 Python,需要一些帮助来完成我的课堂作业.

I am just learning python and need some help for my class assignment.

我有一个包含文本和数字的文件.有些行有 1 到 3 个数字,有些则根本没有数字.

I have a file with text and numbers in it. Some lines have from one to three numbers and others have no numbers at all.

我需要:

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

  1. Extract numbers only from the file using regex

求所有数之和

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

I used regex to extract out all the numbers. I am trying to get the total sum of all the numbers but I am just getting the sum of each line that had numbers. I have been battling with different ways to do this assignment and this is the closest I have gotten to getting it right.

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

I know I am missing some key parts but I am not sure what I am doing wrong.

这是我的代码:

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

我得到的结果是这样的(每个都是一行的总和):

The result I get is something like this(each is a sum of a line):

14151

8107

16997

18305

3866

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

And it needs to be one sum like this (sum of all numbers):

134058

推荐答案

import re
import np
text = open('text_numbers.txt')
final = []
for line in text:
    line = line.strip()
    y = re.findall('([0-9]+)',line)

    if len(y) > 0:
         lineVal = sum(map(int, y))
         final.append(lineVal)
         print "line sum = {0}".format(lineVal)
 print "Final sum = {0}".format(np.sum(final))

这就是你要找的吗?

这篇关于使用正则表达式从文本文件中提取的字符串总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 01:20