python - x.lower() 使我的字符串可联合。为什么

标签 python lowercase

我在 python 中创建了一个代码来搜索文档中的单词。文档存储为字典,以“url”作为键,内容作为值。我想在执行搜索操作之前将文档文本转换为小写。

def get_page(url):
    if url in cache:
        x=cache[url]
        return x.lower()
    else:
        return None

我使用了这段代码。 cache 是字典,url 是键。 但我收到此错误...

Line 324: TypeError: Cannot call method 'tp$iter' of null

我认为这是一个不可分割的字符串。

这里第一行发生错误

for char in source:
    if char in splitlist:
        atsplit = True

source 与上面引用的函数中的 x 相同。

最佳答案

确保在返回 None 时不会陷入循环 for char in source,因为 None 不可迭代。您也可以返回 "",而不是返回 None,具体取决于您如何使用 get_page 函数

您的假设不正确的简单演示

for char in "google.com".lower():
    print(char)


# python test.py
g
o
o
g
l
e
.
c
o
m

关于python - x.lower() 使我的字符串可联合。为什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14032529/

相关文章:

python - Pygame 图像屏幕填充

python - Tkinter 和 Tix 演示或展示应用程序?

mysql - concat_ws 和小写和大写

javascript - 为什么 toLowerCase() 反转文本——为什么?

python - 为 python Web 应用程序运行 apache 服务器时导入错误

javascript - 与经过验证的位置进行交互? - 谷歌地图

python - 使用 Python 的单元测试子测试时避免重复测试?

c++ - 接受用户输入无论他们在 C++ 中输入大写还是小写

PHP - 字符串的所有组合大小写字符

neo4j - 在 Neo4j 中将所有属性的所有值转换为小写