python - 调用函数时,Python 3 中未列出 TypeError : must be str,

标签 python python-3.x

我创建了一个函数来计算字母,例如字母 e。我的函数看起来与此类似:

def count_letter(sentence, accents, case):

    lower_case_e = ['e']
    upper_case_E = ['E']
    accent_lower_case = ['é', 'ê', 'è']
    accent_upper_case = ['É', 'Ê', 'È']

    for character in sentence:#If statement for optional argument where ignore_accents == True and ignore_case == False.
        #This loop will count lower case and upper case e as differente letters but will treat accented characters the same.

        if accents == True and case == False:
            lower_case_count = sentence.count(lower_case_e)
            accent_lower_case_count = sentence.count(accent_lower_case)
            upper_case_count = sentence.count(upper_case_E)
            accent_upper_case_count = sentence.count(accent_upper_case)

            total_e_count = lower_case_count + accent_lower_case_count
            total_E_count = upper_case_count + accent_upper_case_count

            return {'Total number of lower case e in sentence ignoring accents':total_e_count, 'Total number of upper case E in sentence ignoring accents':total_E_count }

这个函数的要点是计算字母 e 的数量,并根据它是小写还是大写或者是否有重音符号,将字母加在一起。我创建了一个名为 Sentence.txt 的文本文件,它看起来像这样:

Testing if function can count letter e or E.

我已使用以下代码读取了该文件:

# Reading the data from sentence.txt as a string
with open('sentence.txt', 'r') as Sentence_file:
    Sentence_string=Sentence_file.read().replace('\n', '')

阅读文件后,我尝试按以下方式调用该函数:

count_letter(sentence, True, False)

但是,当我尝试运行此程序时,出现以下错误:

TypeError: must be str, not list

有人知道可能出了什么问题吗?错误可能出在我读取 txt.file 的方式上吗?任何建议将不胜感激!

完整的错误跟踪如下所示:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-2-b171590ddd67> in <module>()
     29 with open('Test_Sentence1.txt', 'r') as Sentence1_file:
     30     Sentence1=Sentence1_file.read().replace('\n', '')
---> 31 count_letter_e(Sentence1, True, False)
     32 

<ipython-input-2-b171590ddd67> in count_letter_e(sentence, accents, case)
     18         if accents == True and case == False:#If statement for optional argument where ignore_accents == True and ignore_case == False.
     19             #This loop will count lower case and upper case e as differente letters but will treat accented characters the same.
---> 20             lower_case_count = sentence.count(lower_case_e)#counting lower case e with no accent from the sentence
     21             accent_lower_case_count = sentence.count(accent_lower_case)#counting lower case e with accents from the sentence
     22             upper_case_count = sentence.count(upper_case_E)#counting upper case E with no accent from the sentence

TypeError: must be str, not list

最佳答案

count()”函数仅接受字符串作为输入。例如:

lower_case_count = 0
for lower_case_e_char in lower_case_e:
    lower_case_count += sentence.count(lower_case_e_char)
print(lower_case_count)

关于python - 调用函数时,Python 3 中未列出 TypeError : must be str,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46857703/

相关文章:

python - 在 Python 中解析带有未声明前缀的 XML

python - Scapy BPF 过滤器不工作

python - 检查 2D NumPy 数组的坐标是否在特定范围内的优雅方法

python-3.x - Pandas 选择无法转换为 int 或 float 的行

python - Beautiful Soup and Table Scraping - lxml 与 html 解析器

python - 如何使用 Python 添加新行并将其填充到现有 excel 文件中

python - Python的Threading模块的处理顺序

python - 为什么从生成器内调用模拟对象无法正确测试?

python - 在 Python 中测试生成器

python - 对于 python2/3 向后兼容调用 super()