簡體   English   中英

我該怎么辦?(類型錯誤)

[英]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)

這段代碼給了我這個錯誤: with open("regex_sum_340933.txt", "r", "UTF-8") as some_file: TypeError: an integer is required (got type str)

open的第三個參數是buffering請參閱文檔),而不是encoding ,因此您需要將encoding作為關鍵字參數傳遞。 另外,最好使用"utf8"

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM