我该怎么办?(TypeError)

时间:2020-05-15 10:05:14

标签: python-3.x typeerror

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)

1 个答案:

答案 0 :(得分:1)

open的第三个参数是bufferingsee the docs),而不是encoding,因此您需要将encoding作为关键字参数传递。另外,最好使用"utf8"

with open("regex_sum_340933.txt", "r" , encoding="utf8") as some_file: