对于此代码,我试图列出以New Revision开头的行中的所有数字。我设法如下所示在“过滤的x值”下执行此操作。但是,即使我将x数据类型从字符串更改为整数,我仍然需要将所有“已过滤的x值”相加,因为它会一直提示类型错误。这可能有什么问题?
import re
hand = open('D:/Python/mbox-short.txt')
count = []
y = list()
k = 0
a = 0
for line in hand:
line = line.rstrip()
x = re.findall('^New Revision: ([0-9.]+)', line)
if len(x) > 0:
k = (x)
a = a + int(k)
print(k) //This is a list of filtered values (x)
print(a)
过滤的x值
['39772']
['39771']
['39770']
['39769']
['39766']
['39765']
['39764']
['39763']
['39762']
['39761']
['39760']
['39759']
['39758']
['39757']
['39756']
['39755']
['39754']
['39753']
['39752']
['39751']
['39750']
['39749']
['39746']
['39745']
['39744']
['39743']
['39742']
TypeError Traceback (most recent call last)
<ipython-input-247-8432b00ac729> in <module>()
10 if len(x) > 0:
11 k = (x)
---> 12 a = a + int(k)
13 print(k)
14 print(a)
TypeError: int() argument must be a string, a bytes-like object or a number,
not 'list'