python - 在 Python 中查找文本中所有出现的整数

标签 python regex string substring

我编写这段代码的目的是从文本中提取所有整数并将它们相加。

我一直在寻找解决方案来提取一行文本中的所有整数。我看到一些建议使用 \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

最佳答案

  1. 需要调用urllib2.urlopen返回值的readurllib2.urlopen的返回值不是字符串,而是一个连接对象(类文件对象)
  2. 只需将re.findall 应用于数据
  3. \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/

相关文章:

ruby - 字符串问题的符号

c++ - 来自字符串的eof,而不是流

python - 如何在 Django 中按日期对对象进行分组?

python - 在图中查找所有长度为 2 的路径

python - AppEnginePlatformWarning - 使用套接字的原因?

python - python : is it possible to get the match,替换中的正则表达式和最终字符串?

javascript - 如何判断日期字符串是否包含已在 Javascript 中添加的时间偏移量?

c#用do替换字符串包含未知部分

r - 在 R 中提取值(重复)

python - SSH 服务器按用户路由隧道