【问题标题】:How can i compare if a text line starts with a word in Python? [duplicate]如何比较文本行是否以 Python 中的单词开头? [复制]
【发布时间】:2019-11-07 16:49:25
【问题描述】:

我有一个关键文本“Outros”和一个正在阅读和枚举的文本文件。我只需要打印以该键开头的行。有可能吗?

我已经在打印行了,但它会打印行内包含此键的每一行,而不仅仅是开始。我真的不知道我该怎么做。

for index, line in enumerate(lines):
    if a in line:
        print(line.rstrip())
    if b in line:
        print(line.rstrip())

【问题讨论】:

  • 一个字符串对象有startswith的方法。

标签: python python-3.x text-extraction


【解决方案1】:
for index, line in enumerate(lines):
    if line.startswith('Outros'):
        print(line)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-09
    相关资源
    最近更新 更多