python Coursera - 打开文本文件并使用正则表达式计算嵌入在文本中的数字。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python Coursera - 打开文本文件并使用正则表达式计算嵌入在文本中的数字。相关的知识,希望对你有一定的参考价值。

import re
import os

os.getcwd()
os.chdir("C:\\Users\\Jarrod\\Documents\\Python Scripts")

file = open("countnumbers.txt")
numbers = list()
num = list()
for line in file:
    line = line.rstrip()
    match = re.findall('([0-9]+)', line)
    if len(match) == 0: continue
    for i in match:
        num.append(int(i))
print sum(num)

以上是关于python Coursera - 打开文本文件并使用正则表达式计算嵌入在文本中的数字。的主要内容,如果未能解决你的问题,请参考以下文章