我编写这段代码的目的是从文本中提取所有整数并将它们相加。
我一直在寻找解决方案来提取一行文本中的所有整数。我看到一些建议使用 \D
和 \b
的解决方案,我刚刚开始使用正则表达式,但仍然不熟悉它如何适合我的代码。请帮助:(
import re
import urllib2
data = urllib2.urlopen("http://python-data.dr-chuck.net/regex_sum_179860.txt")
aList = []
for word in data:
data = (str(w) for w in data)
s = re.findall(r'[\d]+', word)
if len(s) != 1: continue
num = int(s[0])
aList.append(num)
print aList
最佳答案
- 需要调用
urllib2.urlopen
返回值的read
;urllib2.urlopen
的返回值不是字符串,而是一个连接对象(类文件对象) - 只需将
re.findall
应用于数据
。 \d
不需要方括号。
import re
import urllib2
data = urllib2.urlopen("http://python-data.dr-chuck.net/regex_sum_179860.txt").read()
int_list = map(int, re.findall(r'\d+', data))
>>> int_list
[3524, 9968, 6177, 3133, 6508, 7940, 3738, 1112, 6179, 4570, 6127, 9150,
9883, 418, 3538, 2992, 8527, 1150, 2049, 2834, 2630, 3840, 2638, 3800,
9144, 5866, 6742, 588, 6918, 7802, 8229, 7947, 8992, 1339, 2119, 846,
3820, 4070, 9356, 9708, 3238, 9380, 5572, 9491, 3038, 7434, 7771, 288,
8632, 3962, 9136, 8106, 7295, 3699, 4136, 3459, 8120, 6018, 8963, 5779,
3635, 3984, 4850, 9633, 2588, 7631, 9591, 1067, 7182, 1301, 8041, 1361,
5425, 8326, 7094, 8155, 2581, 7199, 6125, 42]
关于python - 在 Python 中查找文本中所有出现的整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34315342/