Sum of strings extracted from text file using regex

后端 未结 8 1143
梦毁少年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:30

    I dont know much python but I can give a simple solution. Try this

    import re
    hand = open('text_numbers.txt')
    x=list()
    for line in hand:
        y=re.findall('[0-9]+',line)
        x=x+y
    sum=0
    for i in x:
        sum=sum + int(i)
    print sum
    

提交回复
热议问题