简体   繁体   中英

What should I do?(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)

This code gives me this error: with open("regex_sum_340933.txt", "r", "UTF-8") as some_file: TypeError: an integer is required (got type str)

open 's third argument is buffering ( see the docs ), not encoding , so you need to pass encoding as a keyword argument. Also, it's better to use "utf8" .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM