首页
\
问答
\
关键字搜索并导出到Python中输出.txt文件中的doc(Keyword Search and exporting to output doc in .txt file in Python)
关键字搜索并导出到Python中输出.txt文件中的doc(Keyword Search and exporting to output doc in .txt file in Python)
我试图编写一个python函数来读取文本文件的每一行,并搜索关键字:如果关键字在行中,我试图将行导出到新的文本文档中。 这本质上会创建一种过滤文本行的方式。 这是我到目前为止:
#trying to filter and print a line that contains a keyword in a csv or txt file import subprocess import csv def findandsearch(word): textfile=open('textHCPC17_CONTR_ANWEB.txt', 'r+') #openign the selected text file outputfile=input('CMSOUTPUT.txt') #classifying an ouput file word=s'education' #designating a keyword for line in textfile: textfile.readline() if word in textfile: #creating if clause print('this is a match') #printing that this is match outputfile.write(word) #I want to write the selected line of the text in the output file textfile.close() #closing the original file print(word) #I want to print the results return #ending function
任何帮助将不胜感激,因为我没有遇到语法错误,但我的输出文件是空白的。
I'm trying to write a python function that reads each individual line of a text file and searches for a keyword: if the keyword is in the line I'm trying to make export the line into a new text document. This would essentially create a way to filter lines of text. Here is what I have so far:
#trying to filter and print a line that contains a keyword in a csv or txt file import subprocess import csv def findandsearch(word): textfile=open('textHCPC17_CONTR_ANWEB.txt', 'r+') #openign the selected text file outputfile=input('CMSOUTPUT.txt') #classifying an ouput file word=s'education' #designating a keyword for line in textfile: textfile.readline() if word in textfile: #creating if clause print('this is a match') #printing that this is match outputfile.write(word) #I want to write the selected line of the text in the output file textfile.close() #closing the original file print(word) #I want to print the results return #ending function
Any help would be appreciated as I am not running into syntax error but my output file is blank.
原文:https://stackoverflow.com/questions/45378168
更新时间:2022-08-18 10:08