python - 剥离不删除标签
问题描述
我的理解是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') :
解决方案
看来 Carcigenicate 得到了它。如果我将该行修改为:
line = line.strip()
这解决了它。嗬!
推荐阅读
- frameworks - 如何使用 ADO.Net 实体数据模型从存储过程“ObjectResult”创建用于 RDLC 报告的数据集
- sql-server - 从字符串中提取整数以转换为 Int
- java - 使用 JDA,我想为我的机器人创建一个警告系统
- angular - combineLatest 不适用于承诺
- javascript - 为什么我的切换菜单功能不适用于 div(而不是按钮)已修复
- android - 当我返回上一页时 MediaPlayer 继续播放
- python - 如何在 Amazon EMR 上安装多个版本的 numpy 以及如何删除早期版本?
- java - Java:JTable 中组合框中的索引选择
- python - 将 python 脚本转换为 Windows 的可执行文件或图标?
- python - 为什么淡入淡出动画不起作用但淡出动画起作用?