Sum of strings extracted from text file using regex

后端 未结 8 1146
梦毁少年i
梦毁少年i 2020-12-10 19:47

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

I have a file with text and numbers in it. Some lines have from one to three numbers and other

8条回答
  •  醉话见心
    2020-12-10 20:15

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

提交回复
热议问题