import re
ffail = ""
with open("regex_sum_340933.txt", "r" , "UTF-8") as some_file:
ffail = some_file.read()
count = 0
match = re.findall('[0-9]+', ffail)
for II in match:
number = int(II)
count = count + number
print(count)
这段代码给我这个错误: 使用open(“ regex_sum_340933.txt”,“ r”,“ UTF-8”)作为some_file: TypeError:必须为整数(类型为str)
答案 0 :(得分:1)
open
的第三个参数是buffering
(see the docs),而不是encoding
,因此您需要将encoding
作为关键字参数传递。另外,最好使用"utf8"
。
with open("regex_sum_340933.txt", "r" , encoding="utf8") as some_file: