坚持基本正则表达式

时间:2015-12-10 11:16:43

标签: regex python-2.7

任务:查找文本文件中的所有数字并计算其总和。

链接到文件(如果需要):http://python-data.dr-chuck.net/regex_sum_42.txt

name = raw_input("Enter your file: ")
if len(name) < 1: name = "sample.txt"

try: 
    open(name)
except:
    print "Please enter a valid file name."
    exit()

import re
lst = list()
for line in name:
    line = line.strip()  #strip() instead of rstrip() as there were space before line as well
    stuff = re.findall("[0-9]+", line)
    print stuff               # i tried to trace back and realize it prints empty list so problem should be here
    stuff = int(stuff[0])     # i think this is wrong as well 
    lst.append(stuff)
    sum(lst)

print sum(lst)

有人能告诉我哪里出错了吗?抱歉任何格式错误并感谢您的帮助

我也尝试过:

\s[0-9]+\s
.[0-9]+.

1 个答案:

答案 0 :(得分:1)

您需要将代码更改为:

lst = []
with open(name) as f:
    for line in f:
        stuff = [lst.append(int(x)) for x in re.findall("[0-9]+", line.strip())]
print sum(lst)

请参阅IDEONE demo

问题是你试图首先解析一个空字符串。解析为int并附加到 comprehension 中的列表(用lst = []声明)时,可以避免混淆空输出,并且自动缩放列表。

此外,您需要实际读取该文件。with语句处理打开和关闭文件,包括是否在内部块中引发异常。for line in f对待文件对象f作为可迭代,它自动使用缓冲的IO和内存管理,因此您不必担心大文件。“source