简体   繁体   English

如何使用 Python 仅打印文本文件中字符串的第一个实例?

[英]How do I print only the first instance of a string in a text file using Python?

I am trying to extract data from a .txt file in Python.我正在尝试从 Python 中的 .txt 文件中提取数据。 My goal is to capture the last occurrence of a certain word and show the next line, so I do a reverse () of the text and read from behind.我的目标是捕获某个单词的最后一次出现并显示下一行,因此我对文本进行了反向 () 并从后面阅读。 In this case, I search for the word 'MEC', and show the next line, but I capture all occurrences of the word, not the first.在这种情况下,我搜索“MEC”这个词,并显示下一行,但我捕获了该词的所有出现,而不是第一个。

Any idea what I need to do?知道我需要做什么吗?

Thanks!谢谢!

This is what my code looks like:这是我的代码的样子:

import re

from file_read_backwards import FileReadBackwards

with FileReadBackwards("camdex.txt", encoding="utf-8") as file:
    for l in file:
        lines = l

        while line:
            if re.match('MEC', line):
                x = (file.readline())
                x2 = (x.strip('\n'))
                print(x2)
                break
            line = file.readline()

The txt file contains this: txt 文件包含以下内容:

MEC
29/35
MEC
28,29/35

And with my code print this output:用我的代码打印这个输出:

28,29/35
29/35

And my objetive is print only this:我的目标是只打印这个:

28,29/35

Get rid of the extra imports and overhead.摆脱额外的导入和开销。 Read your file normally, remembering the last line that qualifies.正常阅读您的文件,记住符合条件的最后一行。

with ("camdex.txt", encoding="utf-8") as file:
    for line in file:
        if line.startswith("MEC"):
            last = line

print(last[4:-1])    # "4" gets rid of "MEC "; "-1" stops just before the line feed.

If the file is very large, then reading backwards makes sense -- seeking to the end and backing up will be faster than reading to the end.如果文件非常大,那么向后读取是有意义的——寻找到最后并备份将比读取到最后更快。

This will give you the result as well.这也会给你结果。 Loop through lines, add the matching lines to an array.循环遍历行,将匹配的行添加到数组中。 Then print the last element.然后打印最后一个元素。

import re
with open("data\camdex.txt", encoding="utf-8") as file:
    result = []
    for line in file:
        if re.match('MEC', line):
            x = file.readline()
            result.append(x.strip('\n'))

print(result[-1])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Python在文本文件中找到字符串的第一个实例? - How do I find the first instance of a string in a text file using Python? 如何使用 Python 仅打印 csv 文件的前 10 行? - How do I print only the first 10 lines from a csv file using Python? Python:仅从文本文件中删除字符串的第一个实例 - Python: Remove first instance only of string from text file 如何在 Python 中打印文本文件每个段落的第一行? - How do I print the first line of each paragraph of a text file in Python? 在Python中:如何将文本文件中的元素打印为字符串,就像出现在文件中一样? - In Python: How do I print the elements as a string from a text file the same way as it appears in the file? 如何逐行读取文件并仅在python中打印具有特定字符串的行? - How do I read a file line by line and print the line that have specific string only in python? 如何使用Python指定要打印到文本文件的列? - How do I specify which columns to print to a text file using Python? 如何使用 python 中的 if 语句从文本文件中打印信息? - How do I print information from a text file using if statements in python? Python:如何从.txt文件中打印unicode字符串 - Python: How do I print an unicode string from a .txt file 在文本文件中找到一个字符串,然后在Python中打印以下各行的第一个单词 - Find a string in a text file, and then print the first words of the following lines in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM