首页 > 解决方案 > 剥离不删除标签

问题描述

我的理解是strip():在删除包括制表符 (\t) 在内的任何前导和尾随空格后返回一个新字符串。

我有几行的源文本。一行的开头似乎是一个以“B”字符开头的制表符

豆落温度 332

为什么以下 Python 3 代码不能将其识别为以“Bean”开头的行?

count = 0
for line in fhand:
    # strip beginning and ending whitespace from lines
    line.strip()
    if line.startswith('Bean') :
        count = count + 1
print("There were", count, "lines in the file with Bean as the first word")

如果我将一行更改为包含几个空格,那么它可以工作。

    if line.startswith('  Bean') :

标签: pythonpython-3.x

解决方案


看来 Carcigenicate 得到了它。如果我将该行修改为:

line = line.strip()

这解决了它。嗬!


推荐阅读