简体   繁体   English

如何在文本文件中搜索包含特定单词的行,然后使用“找到”行创建一个新文件

[英]How to search a text file for lines that contain a specific word, then create a new file with the "found" lines

I need to search some data (text file) for lines that contain a specific word, then create a new text file that only contains 'found' lines.我需要在一些数据(文本文件)中搜索包含特定单词的行,然后创建一个仅包含“找到”行的新文本文件。

For example, if the data in the original text file (data.txt) was:例如,如果原始文本文件 (data.txt) 中的数据是:

Child 9  60lbs  Female Diabetes
Adult 25 180lbs Male   ADHD
Adult 46 200lbs Female epilepsy
Child 10 65lbs  Female ADHD

And I was to search for the keyword 'Child', the new text file (output.txt) would be:我要搜索关键字'Child',新的文本文件(output.txt)将是:

Child 9  60lbs  Female Diabetes
Child 10 65lbs  Female ADHD  

This is my code so far, I don't really know how to go about writing the found lines to a new file.到目前为止,这是我的代码,我真的不知道如何将找到的行写入新文件。

def main():
    
    Word1 = 'Child'
    
    #open an existing file. 
    OriginalData = open('recordData.txt', 'r')
    datalist = []

    for line in OriginalData.readlines():
            if Word1 in line: 
                #write the line to new file.
    
if __name__=="__main__":
    main()
search_word = 'your search word here'
with open('data.txt') as file:  # opens it, then closes it automatically at the end of the with block
    lines = [line for line in file.read().split('\n') if search_word in line]

with open('output.txt', 'w+') as output_file:  # 'w+' specifies it can be written to and created
    output_file.write('\n'.join(lines))

Now we can break down lines = [line for line in file.read().split('\n') if search_word in line]现在我们可以分解lines = [line for line in file.read().split('\n') if search_word in line]

file.read() returns a string of the whole file file.read()返回整个文件的字符串

.split('\n') turns the string into a list, breaking it at every newline ( '\n' is a newline) .split('\n')将字符串转换为列表,在每个换行符处打破它( '\n'是换行符)

if search_word in line so it only adds the lines with the words if search_word in line所以它只添加带有单词的行

'\n'.join(lines) puts the elected lines back together, then writes it to the file with write '\n'.join(lines)将选定的行重新组合在一起,然后使用write将其写入文件

暂无
暂无

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

相关问题 定义一个函数来搜索文本文件中的特定单词并打印总行数 - define a function to search for a specific word in a text file and print total lines 如何将文本的特定行写入新文件? - How to write specific lines of a text to a new file? 如何在文本行之间搜索单词并用 Python 在文本文件中替换它? - How to search word in between text lines and replace it in Text file with Python? 如何从文本文件中仅提取具有特定单词的行并编写一个新行? - How to extract only lines with specific word from text file and write a new one? 过滤文本文件中包含特定单词的行 - Filter lines from a text file which contain a particular word 使用一个文件搜索另一个文件,从找到的行创建新文件 - Use one file to search another file, create new file from found lines 除了特定的文本块,如何将行写入新文件? - How to write lines to a new file except for a specific block of text? 尝试删除文本文件中包含特定字符的行 - Trying to delete lines in a text file that contain a specific character 如何检查文本文件的每一行是否有字符串并将所有出现该单词的行打印到新文本文件中? - How to check each line of a text file for a string and print all lines with occurrences of that word to a new text file? 从文本文件中读取行,如果特定单词 == 100,则打印该行 - Read lines from text file and if a specific word == 100, print the line
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM